简体   繁体   English

实体框架代码首先是多层父子外键

[英]Entity Framework code first multiple tier parent child foreign key

I am trying to implement a hierarchy using Entity Framework 6 and I am having trouble when I get past the first level of a parent child relationship. 我正在尝试使用Entity Framework 6实现层次结构,当我超越父子关系的第一级时,我遇到了麻烦。 An example of what I am trying to do is below: 我想要做的一个例子如下:

[Table("TestParent", Schema = "dbo")]
public class TestParent {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestParentId { get; set; }

    private ICollection<TestChild> _testChildren;

    public virtual ICollection<TestChild> TestChildren {
        get { return _testChildren ?? (_testChildren = new HashSet<TestChild>()); }
        set { _testChildren = value; }
    }
}

[Table("TestChild", Schema = "dbo")]
public class TestChild {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestId { get; set; }

    [Key, ForeignKey("TestParent"), Column(Order = 2)]
    public int TestParentId { get; set; }
    public virtual TestParent TestParent { get; set; }

    private ICollection<TestGrandChild> _testGrandChildren;

    public virtual ICollection<TestGrandChild> TestGrandChildren {
        get { return _testGrandChildren ?? (_testGrandChildren = new HashSet<TestGrandChild>()); }
        set { _testGrandChildren = value; }
    }
}

[Table("TestGrandChild", Schema = "dbo")]
public class TestGrandChild {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestGrandChildId { get; set; }

    [Key, Column(Order = 2)]
    public int TestChildId { get; set; }
    public virtual TestChild Test { get; set; }
}

For some reason EF does not recognise that the TestGrandChild.TestChildId is a foreign key to TestChild.TestChildId . 出于某种原因,EF不承认, TestGrandChild.TestChildId是一个外键TestChild.TestChildId If I try to force it like so: 如果我试图像这样强迫它:

[Table("TestGrandChild", Schema = "dbo")]
public class TestGrandChild {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestGrandChildId { get; set; }

    [Key, ForeignKey("TestChild"), Column(Order = 2)]
    public int TestChildId { get; set; }

    public virtual TestChild Test { get; set; }
}

I get the following error: 我收到以下错误:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---> System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: TestGrandChild_TestChild_Target_TestGrandChild_TestChild_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical. ---> System.Data.Entity.ModelConfiguration.ModelValidationException:在模型生成期间检测到一个或多个验证错误:TestGrandChild_TestChild_Target_TestGrandChild_TestChild_Source ::关系约束中的从属角色和主要角色中的属性数必须相同。

Driving me CRAZY!!!! 快把我逼疯!!!!

TestChild has a composite primary key made of (TestId, TestParentId) . TestChild有一个由(TestId, TestParentId)组成的复合主键。 Therefore the foreign key in TestGrandChild refering to TestChild must be composite as well with the same number of parts - that's what the exception is telling. 因此,外键TestGrandChild指的TestChild必须是复合材料,以及与相同数量的部分-这就是异常告诉。

TestGrandChild should look like this: TestGrandChild应如下所示:

[Table("TestGrandChild", Schema = "dbo")]
public class TestGrandChild {

    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestGrandChildId { get; set; }

    [Key, ForeignKey("TestChild"), Column(Order = 2)]
    public int TestId { get; set; }

    [Key, ForeignKey("TestChild"), Column(Order = 3)]
    public int TestParentId { get; set; }

    public virtual TestChild TestChild { get; set; }
}

TestGrandChild now has a composite key (TestGrandChildId, TestId, TestParentId) and the last two parts (TestId, TestParentId) form a composite foreign key to the primary key (TestId, TestParentId) in TestChild . TestGrandChild现在有一个复合键(TestGrandChildId, TestId, TestParentId)和最后两个部分(TestId, TestParentId)形成复合外键的主键(TestId, TestParentId)TestChild

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM