Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. EF Core 6 Exclude Columns

EF Core 6 Exclude Columns

Scheduled Pinned Locked Moved Database
questionasp-nethelp
4 Posts 3 Posters 20 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have an entity table which is begin queried into an entity:

    UserEntity? user = dc.Users
    .Where(x => x.UserName.CompareTo(entity.UserName) == 0)
    .FirstOrDefault();

    Now, if I add a property to the entity that does NOT exist in the underlying table, it throws an exception:

    Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'FullName'.'

    FullName if not a table field. How can I prevent this exception for properties that don't exist in the data?

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    D U 2 Replies Last reply
    0
    • K Kevin Marois

      I have an entity table which is begin queried into an entity:

      UserEntity? user = dc.Users
      .Where(x => x.UserName.CompareTo(entity.UserName) == 0)
      .FirstOrDefault();

      Now, if I add a property to the entity that does NOT exist in the underlying table, it throws an exception:

      Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'FullName'.'

      FullName if not a table field. How can I prevent this exception for properties that don't exist in the data?

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      If I remember correctly, you add the [NotMapped] attribute to the property in your model class.

      public class User
      {
      public string FirstName { get; set; }
      public string LastName { get; set; }
      [NotMapped]
      public string FullName { get; set; } // or whatever your property definition is
      }

      You could easily find this yourself just by Googling for "Entity Framework ignore property".

      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
      Dave Kreskowiak

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        If I remember correctly, you add the [NotMapped] attribute to the property in your model class.

        public class User
        {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        [NotMapped]
        public string FullName { get; set; } // or whatever your property definition is
        }

        You could easily find this yourself just by Googling for "Entity Framework ignore property".

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Thanks. I spent time on Google looking around, but I wasn't really sure what I was looking for.

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        • K Kevin Marois

          I have an entity table which is begin queried into an entity:

          UserEntity? user = dc.Users
          .Where(x => x.UserName.CompareTo(entity.UserName) == 0)
          .FirstOrDefault();

          Now, if I add a property to the entity that does NOT exist in the underlying table, it throws an exception:

          Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'FullName'.'

          FullName if not a table field. How can I prevent this exception for properties that don't exist in the data?

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          U Offline
          U Offline
          unitedsol zain
          wrote on last edited by
          #4

          There are a few ways you can prevent the exception from being thrown when trying to access a property that does not exist in the underlying table:

          Use the "select" statement to select only the properties that exist in the table:
          Copy code

          UserEntity? user = dc.Users
          .Where(x => x.UserName.CompareTo(entity.UserName) == 0)
          .Select(x => new UserEntity { UserName = x.UserName, Email = x.Email })
          .FirstOrDefault();
          Use the "dynamic" keyword to avoid the compile-time check for the property:
          Copy code
          dynamic user = dc.Users
          .Where(x => x.UserName.CompareTo(entity.UserName) == 0)
          .FirstOrDefault();

          Use a try-catch block to catch the exception and handle it:
          Copy code

          try {
          UserEntity? user = dc.Users
          .Where(x => x.UserName.CompareTo(entity.UserName) == 0)
          .FirstOrDefault();
          } catch (SqlException ex) {
          // handle the exception here
          }

          You can check if property exists before accessing it:
          Copy code
          if(entity.GetType().GetProperty("FullName") != null)
          {
          //use the property
          }

          It's important to note that the first and second approach may have an impact on the performance of the application.

          In general, it is best to use the first approach (selecting only the properties that exist in the table) to ensure that the application is only querying the necessary data and to avoid any unexpected behavior.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups