7

Używam Entity Framework 5 code first. W mojej bazie danych mam 2 tabele, AvailPayPeriods i AvailPayPeriodsWeekly. Obaj wyglądają tak samo:Jak skonfigurować wiele zestawów obiektów dla każdego typu w kodzie Entity Framework najpierw

Period datetime not null 

Ponieważ te 2 stoły są identyczne w naturze zdecyduję się utworzyć następujące klasy do reprezentowania albo 1 z 2:

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

    public DateTime Period { get; set; } 
} 

jestem stara się skonfigurować 2. mam następujących w moim klasy kontekstu bazy danych:

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 
} 

Moja WeeklyPayPeriodConfiguration klasa:

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

Kiedy zadzwonić do mojego repozytorium wrócić tygodniowe okresy płac pojawia się następujący błąd:

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

Jak mogę zmapować 2 do odpowiednich tabel?

Czy powinienem raczej utworzyć oddzielne klasy o nazwach WeeklyPayPeriod i MonthlyPayPeriod?

Odpowiedz

7

Można dodać następujące klasy:

public class MonthlyPayPeriod : PayPeriod 
{ 

} 

public class WeeklyPayPeriod : PayPeriod 
{ 

} 

i zmieniają swój DbContext do:

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

} 

Nie idealne, ale dostaje zadanie.

+0

Możesz oznaczać klasę PayPeriod jako abstrakt, aby uniknąć pola Discriminator, patrz Tabela na klasę Concrete (TPC): http://www.entityframeworktutorial.net/code-first/inheritance-strategy-incode -first.aspx – Adi

1

Po otrzymaniu zaakceptowanej odpowiedzi pojawia się błąd polegający na dwukrotnym deklarowaniu DbSet tego samego typu. W takim przypadku struktura encji nie może rozstrzygnąć, której instancji DbSet należy użyć dla podmiotów PayPeriod. Używanie oddzielnych klas pozwala EF rozwiązać na poprawny 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; } 

zobacz sekcję: Entity Framework 4 Code Only Error “Multiple objects sets per 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 Dla realizacji Table od rodzaju betonu Mapowanie (TPC).

Powiązane problemy