实体框架通过约定忽略属性 [英] Entity Framework Ignore property by conventions

查看:153
本文介绍了实体框架通过约定忽略属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码优先模型,其中所有实体都是从一个 Entity 基类派生的。我在基类中有一个属性 IsDeleted ,我想在所有实体中忽略(我不能删除/评论IsDeleted属性,因为基类在许多项目中使用)。有没有办法配置 modelBuilder 忽略所有实体的属性(通过约定,我想),而不必指定 modelBuilder.Entity< .. 。>()。从我的模型中忽略(l => l.IsDeleted)

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?

谢谢
离子

推荐答案

您可以使用新的EF 6.1 自定义代码第一个约定

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

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

这将忽略名称 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"));

这篇关于实体框架通过约定忽略属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆