3
我在實體框架中將單個實體映射到兩個不同表中有一些困難,其中一個是可選的,以提供快速概覽。實體框架 - 單個實體到多個表
我有一個主表是我們公司很多應用程序使用它的核心表的主表,所以我們不希望對此表做任何更改。
在我們的新應用程序中,我們需要更多的列來支持我們添加的一些功能。
我創建了一個單一的實體模型,將信息保存到這兩個表,但它工作正常時,這兩個表中有記錄
但對於歷史記錄,這(通過主鍵和外鍵關聯)新表格不會有關聯的記錄,也不能獲取任何實體集合。
下面是代碼片段。
public class ModelTable
{
public string PatientID { get; set; }
public string Diagnosis1 { get; set; }
public string Diagnosis2 { get; set; }
public string Diagnosis3 { get; set; }
public string Diagnosis4 { get; set; }
public string Diagnosis5 { get; set; }
public string Diagnosis6 { get; set; }
public string Diagnosis7 { get; set; }
public string Diagnosis8 { get; set; }
}
public class ModelTableMap : EntityTypeConfiguration<ModelTable>
{
public ModelTableMap()
{
//Table1
this.Map(model =>
{
model.Properties(table1 => new
{
table1.Diagnosis1,
table1.Diagnosis2,
table1.Diagnosis3,
table1.Diagnosis4,
table1.Diagnosis5,
table1.Diagnosis6
});
model.ToTable("Table1");
});
//Optional Table
this.Map(model =>
{
model.Properties(table2 => new
{
table2.Diagnosis7,
table2.Diagnosis8,
});
model.ToTable("Table2");
});
this.HasKey(type => type.PatientID);
this.Property(type => type.PatientID).IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(type => type.Diagnosis1).HasColumnName("Diag1");
this.Property(type => type.Diagnosis1).HasColumnName("Diag2");
this.Property(type => type.Diagnosis1).HasColumnName("Diag3");
this.Property(type => type.Diagnosis1).HasColumnName("Diag4");
this.Property(type => type.Diagnosis1).HasColumnName("Diag5");
this.Property(type => type.Diagnosis1).HasColumnName("Diag6");
this.Property(type => type.Diagnosis1).HasColumnName("Diag7");
this.Property(type => type.Diagnosis1).HasColumnName("Diag8");
}
}
如果我將這些表分成兩個不同的POCO類並指定關係,它工作正常。
但我想用Single Entity來實現這個功能,因爲它是同一張表。
請提供任何指導或如果我做錯了,請裸露我的英語不是那麼好。
感謝 Sathish所在
感謝您的回覆。所以我想在這種情況下,我必須和兩個獨立的實體一起生活,並讓它們相關。 – Sathish 2013-04-11 21:25:42