7

我正在使用Entity Framework 5 code first. 在我的数据库中,我有 2 个表,AvailPayPeriods并且AvailPayPeriodsWeekly. 它们看起来都一样:

Period datetime not null

因为这 2 个表本质上是相同的,所以我决定创建以下类来表示 2 个中的 1 个:

public class PayPeriod : IEntity
{
     public int Id { get; set; }

     public DateTime Period { get; set; }
}

我正在努力配置 2。我的数据库上下文类中有以下内容:

public DbSet<PayPeriod> WeeklyPayPeriods { get; set; }
public DbSet<PayPeriod> MonthlyPayPeriods { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Configurations.Add(new WeeklyPayPeriodConfiguration());
     // Haven't yet created the configuration file for monthly pay periods
}

我的WeeklyPayPeriodConfiguration班级:

class WeeklyPayPeriodConfiguration : EntityTypeConfiguration<PayPeriod>
{
     internal WeeklyPayPeriodConfiguration()
     {
          this.ToTable("AvailPayPeriodsWeekly");
     }
}

当我调用我的存储库以取回每周支付期时,我收到以下错误:

Multiple object sets per type are not supported. The object sets 'WeeklyPayPeriods' and 'MonthlyPayPeriods' can both contain instances of type 'ePaySlips.DomainModel.Entities.PayPeriod'.

如何将 2 映射到各自的表?

我是否应该创建单独的名为WeeklyPayPeriodand的类MonthlyPayPeriod

4

2 回答 2

7

您可以添加以下类:

public class MonthlyPayPeriod : PayPeriod
{

}

public class WeeklyPayPeriod : PayPeriod
{

}

并将您的 DbContext 修改为:

public DbSet<WeeklyPayPeriod> WeeklyPayPeriods { get; set; }
public DbSet<MnthlyPayPeriod> MonthlyPayPeriods { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Entity<WeeklyPayPeriod>().Map(m =>
                                                       {
                                                           m.MapInheritedProperties();
                                                           m.ToTable("AvailPayPeriodsWeekly");
                                                       });
        modelBuilder.Entity<MonthlyPayPeriod>().Map(m =>
                                                       {
                                                           m.MapInheritedProperties();
                                                           m.ToTable("AvailPayPeriodsMonthly");
                                                       });

}

不完美,但可以完成工作。

于 2013-02-15T12:56:08.057 回答
1

在接受的答案之后,使用相同类型两次声明 DbSet 会发生错误。发生这种情况时,实体框架无法解析为 PayPeriod 实体使用哪个 DbSet 实例。使用单独的类允许 EF 解析为正确的 DbSet。

//Error with DbSet<PayPeriod> defined twice    
public DbSet<PayPeriod> WeeklyPayPeriods { get; set; }
public DbSet<PayPeriod> MonthlyPayPeriods { get; set; }

//EF can resolve to each  DbSet, but can't store the baseclass PayPeriods
public DbSet<WeeklyPayPeriod> WeeklyPayPeriods { get; set; }
public DbSet<MnthlyPayPeriod> MonthlyPayPeriods { get; set; }

参见相关:Entity Framework 4 Code Only Error “Multiple objects sets per type is not supported”</a>

http://weblogs.asp.net/manavi/archive/2011/01/03/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type- tpc-and-choosing-strategy-guidelines.aspx用于实现 Table Per Concrete Type Mappings (TPC)。

于 2014-05-27T00:48:28.157 回答