7

Entity Framework 5 code first kullanıyorum. Veritabanımda 2 tablo, AvailPayPeriods ve AvailPayPeriodsWeekly var. Her ikisi de aynı görünür: Ben yapılandırmak için mücadele ediyorumVarlık Çerçeve kodunda ilk başına birden fazla nesne kümesi nasıl yapılandırılır?

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

    public DateTime Period { get; set; } 
} 

: Bu 2 tablolar doğada aynıdır Çünkü

Period datetime not null 

Ya 1 2 temsilen aşağıdaki sınıf oluşturmaya karar 2. benim veritabanı bağlam sınıfında şu var:

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 
} 

benim WeeklyPayPeriodConfiguration sınıfı:

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

ben aşağıdaki hatayı alıyorum haftalık ödeme dönemleri geri almak için benim depo diyoruz:

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

nasıl kendi tablolara 2 harita oluşturuyorsunuz?

WeeklyPayPeriod ve MonthlyPayPeriod adlı sınıfları ayırmayı tercih etmeli miyim?

public class MonthlyPayPeriod : PayPeriod 
{ 

} 

public class WeeklyPayPeriod : PayPeriod 
{ 

} 

ve sizin DBContext değişiklik:

cevap

7

Aşağıdaki sınıfları ekleyebilirsiniz

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

} 

Mükemmel değil ama işi alır.

+0

PayPeriod sınıfını özet olarak işaretlemek isteyebilirsiniz, böylece Diskriminator alanını kullanamazsınız, bkz. Tablo Başı Beton sınıfı (TPC): http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code -first.aspx – Adi

1

Kabul edilen yanıttan sonra hata, DbSet'i aynı tür iki kez bildirmekten kaynaklanır. Bu olduğunda, varlık çerçevesi, PayPeriod varlıkları için hangi DbSet örneğinin kullanılacağını çözemez. Ayrı sınıflar kullanmak EF'in doğru DbSet'e çözümlenmesini sağlar.

//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; } 

Bkz İlgili: Entity Framework 4 Code Only Error “Multiple objects sets per type are not supported” Beton Tip Eşlemeler (TCK) Başına Tablo uygulanması için

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.

İlgili konular