Entity Framework Ignore property by conventions

可紊 提交于 2019-12-10 15:17:52

问题


I have a code-first model where all entities are derived from a Entity base class. I have a property IsDeleted in base class which I want to ignore in all entities (I cannot remove/comment IsDeleted property since base class is used in many projects). Is there a way to configure modelBuilder to ignore this property form all entities (by conventions, I think), without to specify modelBuilder.Entity<...>().Ignore(l => l.IsDeleted) for all entities from my model?

Thanks, Ion


回答1:


You can do this using the new EF 6.1 Custom Code First Conventions:

modelBuilder.Types().Configure(c => c.Ignore("IsDeleted"));

This will ignore any property of the name IsDeleted in any of your types.

If you only want to do this for classes inheriting a certain base class, you can do:

modelBuilder.Types()
            .Where(t => t.IsSubclassOf(typeof(MyBaseClass)))
            .Configure(c => c.Ignore("IsDeleted"));



回答2:


You can use the [NotMapped] annotation on the properties, but that will still need to be added for each entity which isn't the same as only specifying it once and having a convention for ignoring it.



来源:https://stackoverflow.com/questions/22038690/entity-framework-ignore-property-by-conventions

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