Ignore some inherited properties in EF6 code first mapping(.NET4 not .NET4.5)

蓝咒 提交于 2019-12-13 02:26:55

问题


I'm using EF6 code first with .NET4(I should deliver the project on win xp so I couldn't configure it with .NET4.5) in a win Form project.

I have a BaseEntity class that all other entities inherited from it:

public abstract class BaseEntity
{
    public int Id {get; set;}
    public int X {get; set;} 
    public int Y {get; set;} 
}  
public class Calendar:BaseEntity
{
    // properties    
}

How could I Ignore X,Y properties in my all entities without writing following code for each entity?

   modelBuilder.Entity<Calendar>()
            .Ignore(t => t.X)
            .Ignore(t => t.Y)

Note that I couldn't use [NotMapped] attribute because I'm using EF6 with .NET 4.


回答1:


Use EntityTypeConfigurations in stead of modelBuilder.Entity<>:

abstract class BaseEntityMapping : EntityTypeConfiguration<BaseEntity>
{
    public BaseEntityMapping()
    {
        this.Ignore(t => t.X);
        this.Ignore(t => t.Y);
    }
}

class CalendarMapping : BaseEntityMapping
{
    public CalendarMapping()
    {
        // Specific mappings
    }
}

And in OnModelCreating:

modelBuilder.Configurations.Add(new CalendarMapping());


来源:https://stackoverflow.com/questions/24400719/ignore-some-inherited-properties-in-ef6-code-first-mapping-net4-not-net4-5

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