简体   繁体   English

忽略EF6代码优先映射中的某些继承属性(.NET4而不是.NET4.5)

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

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. 我先在.NET4中使用EF6代码(我应该在win xp上交付项目,所以我不能在.NET4.5上配置它)在win Form项目中。

I have a BaseEntity class that all other entities inherited from it: 我有一个BaseEntity类,所有其他实体都从该类继承:

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? 在不为每个实体编写以下代码的情况下,如何忽略所有实体中的X,Y属性?

   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. 请注意,由于我将[NotMapped]与.NET 4配合使用,因此无法使用[NotMapped]属性。

Use EntityTypeConfiguration s in stead of modelBuilder.Entity<> : 使用EntityTypeConfiguration代替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 : OnModelCreating

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

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

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