如何在TPH TPT对象之间映射外键 - 实体框架代码优先

Tom*_*les 3 entity-framework ef-code-first entity-framework-4.3

我们有以下一组对象.

public class Form
{
    public int FormId { get; set; }
    public DateTime DateCreatedOn { get; set; }
    public string Status { get; set; }
}

// This is using TPT inheritance from Form
[Table("FormA")]
public class FormA : Form
{
    public string ExtraInfoA { get; set; }
    public virtual ICollection<Child> Children
}

// This is using TPT inheritance from Form
[Table("FormB")]
public class FormB : Form
{
    public string ExtraInfoB { get; set; }
    public virtual ICollection<Adult> Adults
}

public class Person
{
    public int PersonId { get; set; }
    public int FormId
    public DateTime DateOfBirth { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

// This is using TPH inheritance from Person
public class Adult : Person
{
    public int HowManyCars { get; set; }
    public string NationalInsuranceNo { get; set; }
    [ForeignKey("FormId")]
    public virtual FormB FormB { get; set; }
}

// This is using TPH inheritance from Person
public class Child : Person
{
    public int HowManyToys { get; set; }
    public string SchoolName { get; set; }
    [ForeignKey("FormId")]
    public virtual FormA FormA { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这为形式的3个表Form,FormA以及FormB所有与他们相应的字段.它还创建了1个表Person.

问题是当我们依赖约定并且没有指定该ForeignKey属性时,该Person表包含2个额外的外键列.

但是,当我们指定ForeignKey属性(如上面的代码中)时,我们会收到以下错误消息.

`The foreign key component 'FormId' is not a declared property on type 'Child'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.`
Run Code Online (Sandbox Code Playgroud)

FormId绝对属性,Child所以我不确定出了什么问题.

我们的现实情况比上面的情况复杂得多,所以我现在想要得到它,而不是有多个外键.

很感谢任何形式的帮助.

Lad*_*nka 8

您不能在子实体的父实体和导航属性中定义外键.它们必须都在同一实体中定义.您尝试做的甚至在数据库中无效,因为您不能对列进行条件外键约束 - 对两者都有约束FormA并且FormB将应用于每个记录,并且您将永远无法插入任何记录(因为它总是违反约束FormAFormB).

简而言之:您需要父级中的单个导航属性或每个子级的单独外键.