我在将单个实体映射到实体框架中的两个不同表时遇到了一些困难,其中一个是可选的以提供快速概述。
我有一个主表,它是我们公司的许多应用程序都使用它的核心表,所以我们真的不想对这个表进行任何更改。
在我们的新应用程序中,我们需要更多的列来支持我们正在添加的一些功能。
我创建了一个实体模型,它将信息保存到这两个表中,当这两个表都有记录时它工作正常(由主键和外键相关)
但是对于历史记录,这个新表将没有关联的记录,并且无法获取任何实体集。
下面是代码片段。
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 类并指定它工作正常的关系。
但我想用单一实体来实现这一点,因为它在功能上是同一张表。
请提供任何指导,或者如果我做错了,请坦白我的英语不是那么好。
谢谢萨蒂什