Exclude a field/property from the database with Entity Framework 4 & Code-First

旧巷老猫 提交于 2019-11-27 01:01:08

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.

The EF team is considering adding something like this:

builder.Entity<Employee>().Exclude(e => e.AddressAs);

I suggest you tell leave a comment on the EFDesign blog, requesting this feature :)

Hope this helps

Alex

markwilde

for future reference: you can use data annotations MSDN EF - Code First Data Annotations

[NotMapped]        
public string AddressAs { get; set; }

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:

builder.Entity<Employee>().Ignore(e => e.AddressAs);

It's also possible to add the column you want to ignore as a Shadow Property in the DbContext:

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!