EF Core 6 Exclude Columns
-
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.
-
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.
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 -
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 KreskowiakThanks. 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.
-
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.
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 codeUserEntity? 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 codetry {
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.