【问题标题】:Entity Framework - Single Entity to Multiple Tables实体框架 - 单个实体到多个表
【发布时间】:2013-04-11 10:07:34
【问题描述】:

我在将单个实体映射到实体框架中的两个不同表时遇到了一些困难,其中一个是可选的以提供快速概述。

我有一个主表,它是我们公司的许多应用程序都使用的核心表,所以我们真的不想对这张表进行任何更改。

在我们的新应用程序中,我们需要更多的列来支持我们正在添加的一些功能。

我已经创建了一个实体模型,它将信息保存到这两个表中,当这两个表都有记录(由主键和外键相关)时,它工作正常

但是对于历史记录,这个新表将没有关联的记录,并且无法获取任何实体集。

下面是代码sn-p。

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 类并指定关系它工作正常。

但我想用单一实体来实现这一点,因为它在功能上是同一张表。

请提供任何指导,或者如果我做错了,请坦白我的英语不是很好。

谢谢 萨蒂什

【问题讨论】:

    标签: entity-framework-5


    【解决方案1】:

    当前 EF 版本中的实体拆分需要两个表中的记录。如果要使用实体拆分,则必须为第一个表中的所有现有记录创建空记录。否则不能使用实体拆分。

    【讨论】:

    • 感谢您的回复。所以我想在这种情况下我必须与两个独立的实体一起生活并让它们相关。
    猜你喜欢
    • 2010-11-28
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2014-08-21
    • 1970-01-01
    • 2013-02-18
    相关资源
    最近更新 更多