首先用实体框架代码映射同一类的子项 [英] Mapping child items of same class with Entity Framework Code First
问题描述
我试图使用EF Code First映射一个相当标准的类别模型
public class Category
{
public int ID {get;组; }
public int ParentID {get;组; }
public string Name {get;组; }
public Category ParentCategory {get;组; }
public List< Category> ChildCategories {get;组;
}
我有以下一些东西:
modelBuilder.Entity< Category>()
.HasOptional(t => t.ParentCategory)
.WithMany()
.HasForeignKey(t => t.ParentCategoryID)
.WillCascadeOnDelete();
但这似乎没有照顾ChildCategories?
我是否缺少某些东西?
为了避免重复的问题参数,我遵循以下内容,但并没有完全回答我的具体查询: / p>
Code First Mapping for实体框架层次结构
实体框架CTP5 Code-First Mapping - 同一表中的外键
将您的实体更改为
public class Category
{
public int ID {get;组; }
public int? ParentID {get;组; }
public string Name {get;组; }
public virtual Category ParentCategory {get;组; }
public virtual IList< Category> ChildCategories {get;组; }
}
Make ParentID
可空,并允许 ChildCategories
加载,使其虚拟。
I'm trying to map a fairly "standard" category model using EF Code First
public class Category
{
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public Category ParentCategory { get; set; }
public List<Category> ChildCategories { get; set; }
}
I've got something along the lines of:
modelBuilder.Entity<Category>()
.HasOptional(t => t.ParentCategory)
.WithMany()
.HasForeignKey(t => t.ParentCategoryID)
.WillCascadeOnDelete();
But this doesn't seem to take care of ChildCategories??
Am I missing something?
To avoid the duplicate question argument, I followed the following, however didn't quite answer my specific query:
Code First Mapping for Entity Framework Hierarchy
Entity Framework CTP5 Code-First Mapping - Foreign Key in same table
Change your Entity to
public class Category
{
public int ID { get; set; }
public int? ParentID { get; set; }
public string Name { get; set; }
public virtual Category ParentCategory { get; set; }
public virtual IList<Category> ChildCategories { get; set; }
}
Make ParentID
nullable and to allow ChildCategories
to be lazy loaded, make it virtual.
这篇关于首先用实体框架代码映射同一类的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!