【发布时间】:2015-06-08 20:41:11
【问题描述】:
我正在尝试弄清楚如何配置 EF 来处理以下涉及一个父母和多个孩子的情况:
public class Parent
{
public int ParentId {get; set;}
[ForeignKey("SpecialChildOneId")]
public virtual Child SpecialChildOne {get; set;}
public int? SpecialChildOneId {get; set;}
[ForeignKey("SpecialChildTwoId")]
public virtual Child SpecialChildTwo {get; set;}
public int? SpecialChildTwoId {get; set;}
public virtual ICollection<Child> Children {get; set;}
}
public class Child
{
public int ChildId {get; set;}
public int ParentId {get; set;}
}
为我的父类生成的表看起来不错。在我的子表上,虽然我添加了两个额外的列,Parent_ParentId 和 Parent_ParentId1,但 ParentId 上没有设置外键。
如何强制 ParentId 成为 Child 表上的唯一外键并防止添加其他外键?
【问题讨论】:
标签: c# entity-framework ef-code-first entity-framework-6 one-to-many