7

Sto usando Entity Framework 5 code first. Nel mio database ho 2 tabelle, AvailPayPeriods e AvailPayPeriodsWeekly. Entrambi hanno lo stesso aspetto:Come configurare più insiemi di oggetti per tipo nel codice Entity Framework prima

Period datetime not null 

Poiché queste 2 tabelle sono identiche in natura decido di creare la seguente classe di rappresentare o 1 del 2:

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

    public DateTime Period { get; set; } 
} 

sto lottando per configurare la 2. ho la seguente nel mio database classe del contesto:

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 
} 

mio WeeklyPayPeriodConfiguration classe:

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

Quando chiamo il mio repository per ottenere indietro i periodi di paga settimanale ottengo il seguente errore:

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

Come faccio a mappare il 2 alle rispettive tabelle?

Devo creare piuttosto per separare le classi chiamate WeeklyPayPeriod e MonthlyPayPeriod?

risposta

7

Si potrebbe aggiungere le seguenti classi:

public class MonthlyPayPeriod : PayPeriod 
{ 

} 

public class WeeklyPayPeriod : PayPeriod 
{ 

} 

e modificare la vostra DbContext a:

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

} 

Non perfetto, ma ottiene il lavoro fatto.

+0

È possibile contrassegnare la classe PayPeriod come astratta in modo da evitare il campo Discriminator, consultare Tabella per classe Concrete (TPC): http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code -first.aspx – Adi

1

In seguito alla risposta accettata, l'errore si verifica dichiarando DbSet due volte con lo stesso tipo. In questo caso, il framework di entità non può risolvere quale istanza di DbSet utilizzare per le entità PayPeriod. L'utilizzo di classi separate consente a EF di risolvere il DbSet corretto.

//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 correlati: 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 Per attuare tabella per ogni tipo di calcestruzzo Mapping (TPC).

Problemi correlati