本文介绍了在EF6代码首先映射中忽略一些继承的属性(.NET4不是.NET4.5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$(我应该在win xp上交付项目,所以我无法使用.NET4.5配置) b $ b

我有一个BaseEntity类,所有其他实体从它继承:

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

我如何忽略我所有实体中的X,Y属性,而不必为每个实体编写以下代码?

  modelBuilder.Entity< Calendar>()
.Ignore(t => tX)
.Ignore(t => tY)

请注意,我不能使用 [NotMapped] 属性,因为我使用EF6使用.NET 4。

解决方案

使用 EntityTypeConfiguration code> modelBuilder.Entity<> :

 抽象类BaseEntityMapping:EntityTypeConfiguration< BaseEntity> ; 
{
public BaseEntityMapping()
{
this.Ignore(t => t.X);
this.Ignore(t => t.Y);
}
}

class CalendarMapping:BaseEntityMapping
{
public CalendarMapping()
{
//具体映射
}
}

而在 OnModelCreating

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


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.

解决方案

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

这篇关于在EF6代码首先映射中忽略一些继承的属性(.NET4不是.NET4.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:48