How to set up multiple sets of objects for each type in Entity Framework code first

I am using Entity Framework 5 code first . There are 2 tables in my database, AvailPayPeriods and AvailPayPeriodsWeekly . Both of them look the same:

 Period datetime not null 

Since these two tables are identical in nature, I decided to create the following class to represent either 1 of 2:

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

I am trying to configure 2. In my database context context, there is the following:

 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 } 

My WeeklyPayPeriodConfiguration class:

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

When I call my repository to return weekly pay periods, I get the following error:

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

How to match 2 with the corresponding tables?

Should I create separate classes called WeeklyPayPeriod and MonthlyPayPeriod ?

+7
source share
2 answers

You can add the following classes:

 public class MonthlyPayPeriod : PayPeriod { } public class WeeklyPayPeriod : PayPeriod { } 

and change your DbContext to:

 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"); }); } 

Not perfect, but doing its job.

+7
source

After the accepted answer, an error occurs when declaring DbSet twice with the same type. When this happens, the entity infrastructure cannot decide which instance of DbSet to use for PayPeriod objects. Using separate classes allows EF to resolve the correct 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; } 

See Related: Entity Framework 4 Code Error "Multiple sets of objects for each type are not supported.

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 For implementing table type mappings (TPC).

+1
source

All Articles