简体   繁体   English

实体框架按约定忽略属性

[英]Entity Framework Ignore property by conventions

I have a code-first model where all entities are derived from a Entity base class. 我有一个代码优先模型,其中所有实体都派生自Entity基类。 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). 我在基类中有一个属性IsDeleted ,我想在所有实体中忽略(我不能删除/注释IsDeleted属性,因为在许多项目中使用了基类)。 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? 有没有办法配置modelBuilder来忽略所有实体的这个属性(通过惯例,我认为),而不指定modelBuilder.Entity<...>().Ignore(l => l.IsDeleted)来自我的所有实体模型?

Thanks, Ion 谢谢,离子

You can do this using the new EF 6.1 Custom Code First Conventions : 您可以使用新的EF 6.1 自定义代码优先约定执行此操作:

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

This will ignore any property of the name IsDeleted in any of your types. 这将忽略任何类型中IsDeleted名称的任何属性。

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"));

您可以在属性上使用[NotMapped]注释,但仍然需要为每个实体添加,这与仅指定一次并具有忽略它的约定不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM