I will like to know that is there a way to exclude some fields from the database? For eg:
我想知道有没有办法从数据库中排除一些字段?例如:
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string FatherName { get; set; }
public bool IsMale { get; set; }
public bool IsMarried { get; set; }
public string AddressAs { get; set; }
}
How can I exclude the AddressAs field from the database?
如何从数据库中排除AddressAs字段?
4 个解决方案
#1
20
In the current version the only way to exclude a property is to explicitly map all the other columns:
在当前版本中,排除属性的唯一方法是显式映射所有其他列:
builder.Entity<Employee>().MapSingleType(e => new {
e.Id,
e.Name,
e.FatherName,
e.IsMale,
e.IsMarried
});
Because AddressAs is not referenced it isn't part of the Entity / Database.
由于未引用AddressAs,因此它不是实体/数据库的一部分。
The EF team is considering adding something like this:
EF团队正在考虑添加以下内容:
builder.Entity<Employee>().Exclude(e => e.AddressAs);
I suggest you tell leave a comment on the EFDesign blog, requesting this feature :)
我建议你告诉我在EFDesign博客上留言,请求这个功能:)
Hope this helps
希望这可以帮助
Alex
亚历克斯
#2
53
for future reference: you can use data annotations MSDN EF - Code First Data Annotations
供将来参考:您可以使用数据注释MSDN EF - 代码优先数据注释
[NotMapped]
public string AddressAs { get; set; }
#3
31
I know this is an old question but in case anyone (like me) comes to it from search...
我知道这是一个老问题,但万一有人(像我一样)从搜索中找到它...
Now it is possible in entity framework 4.3 to do this. You would do it like so:
现在可以在实体框架4.3中执行此操作。你会这样做:
builder.Entity<Employee>().Ignore(e => e.AddressAs);
#4
0
It's also possible to add the column you want to ignore as a Shadow Property in the DbContext:
也可以在DbContext中将要忽略的列添加为Shadow属性:
builder.Entity<Employee>().Property<string>("AddressAs");
Then you can query on that column like so:
然后您可以像这样查询该列:
context.Employees.Where(e => EF.Property<string>(e, "AddressAs") == someValue);
#1
20
In the current version the only way to exclude a property is to explicitly map all the other columns:
在当前版本中,排除属性的唯一方法是显式映射所有其他列:
builder.Entity<Employee>().MapSingleType(e => new {
e.Id,
e.Name,
e.FatherName,
e.IsMale,
e.IsMarried
});
Because AddressAs is not referenced it isn't part of the Entity / Database.
由于未引用AddressAs,因此它不是实体/数据库的一部分。
The EF team is considering adding something like this:
EF团队正在考虑添加以下内容:
builder.Entity<Employee>().Exclude(e => e.AddressAs);
I suggest you tell leave a comment on the EFDesign blog, requesting this feature :)
我建议你告诉我在EFDesign博客上留言,请求这个功能:)
Hope this helps
希望这可以帮助
Alex
亚历克斯
#2
53
for future reference: you can use data annotations MSDN EF - Code First Data Annotations
供将来参考:您可以使用数据注释MSDN EF - 代码优先数据注释
[NotMapped]
public string AddressAs { get; set; }
#3
31
I know this is an old question but in case anyone (like me) comes to it from search...
我知道这是一个老问题,但万一有人(像我一样)从搜索中找到它...
Now it is possible in entity framework 4.3 to do this. You would do it like so:
现在可以在实体框架4.3中执行此操作。你会这样做:
builder.Entity<Employee>().Ignore(e => e.AddressAs);
#4
0
It's also possible to add the column you want to ignore as a Shadow Property in the DbContext:
也可以在DbContext中将要忽略的列添加为Shadow属性:
builder.Entity<Employee>().Property<string>("AddressAs");
Then you can query on that column like so:
然后您可以像这样查询该列:
context.Employees.Where(e => EF.Property<string>(e, "AddressAs") == someValue);