忽略EF CodeFirst中所有已实现的接口属性

时间:2015-07-02 04:48:56

标签: c# entity-framework ef-code-first

不久前,我使用了DataBase First方法和edmx模型。我创建了部分类来扩展edmx生成的域模型的功能。

//Generated by tt in edmx
public partial class DomainObject
{
    public int PropertyA {get; set;}
    public int PropertyB {get; set;}
}

//My own partial that extends functionality on generated one       
public partial class DomainObject: IDomainEntity
{
    public int EntityId {get; set;}
    public int EntityTypeId {get; set;}
    public int DoSomethingWithCurrentEntity()
    {
        //do some cool stuff
        return 0;
    }
}

所有部分人员都在实施界面 IDomainEntity

public interface IDomainEntity
{
    int EntityId {get; set;}
    int EntityTypeId {get; set;}
    int DoSomethingWithCurrentEntity();

    //Another huge amount of properties and functions
}

因此,此接口中的所有属性都未映射到DataBase中的表

现在我已迁移到Code First方法,所有这些属性都试图映射到DataBase。当然我可以使用[NotMapped]属性,但接口和类中的属性数量很大(超过300)并且继续增长。是否有任何方法可以同时忽略所有类的部分或接口的所有属性。

1 个答案:

答案 0 :(得分:4)

您可以使用反射来找出要忽略的属性,然后使用DbContext.OnModelCreating方法中的Fluent API忽略它们:

foreach(var property in typeof(IDomainEntity).GetProperties())
    modelBuilder.Types().Configure(m => m.Ignore(property.Name));
相关问题