【发布时间】:2015-06-13 12:15:52
【问题描述】:
我是实体框架中代码优先方法的新手。我对如何做到这一点有点困惑:
我需要与同一张表的 FK 关系,因此我可以在元素之间建立父 --> 子关系。
这是桌子的模型:
public class BucketGroup
{
public int Id {get;set;} // This is the PK in the Table
public string Name {get;set;}
// Now this the FK, to this Same Table:
public int? BucketGroupId {get;set;}
}
所以我把这个项目设为 Nullable,如果 BucketGroupId 是 NULL 那么我知道它是一个父项目。
我创建了一个测试项目并使用 Database First,模型是这样的:
public partial class Testing
{
public Testing()
{
this.Testing1 = new HashSet<Testing>();
}
public int Id { get; set; }
public Nullable<int> ParentId { get; set; }
public virtual ICollection<Testing> Testing1 { get; set; }
public virtual Testing Testing2 { get; set; }
}
因此,如果我向我的模型添加一个类似的属性,这是否会使其成为 FK 到 PK Id?
public class BucketGroup
{
public int Id {get;set;} // This is the PK in the Table
public string Name {get;set;}
// Now this the FK, to this Same Table:
public int? BucketGroupId {get;set;}
public virtual ICollection<BucketGroup> BucketGroup1 { get; set; }
}
这对吗?
【问题讨论】:
标签: c# entity-framework ef-code-first