简体   繁体   中英

Entity Framework Ignore property by conventions

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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