【发布时间】:2013-01-07 15:21:50
【问题描述】:
在 EF5 中定义模型时遇到问题。我想将ParentId 作为数据库中的外键,但出现错误:
System.Data.Entity.Edm.EdmAssociationType: : 多重性与关系“Category_Parent”中角色“Category_Parent_Target”中的引用约束冲突。由于 Dependent Role 中的所有属性都不可为空,因此 Principal Role 的复数必须为“1”。
这是我的模型:
public class Category
{
public Category()
{
this.Childs = new HashSet<Category>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public int ParentId { get; set; }
public virtual Category Parent { get; set; }
public virtual ICollection<Category> Childs { get; set; }
}
和
modelBuilder.Entity<Category>()
.HasOptional(c => c.Parent)
.WithMany(c => c.Childs)
.HasForeignKey(d => d.ParentId);
【问题讨论】:
-
请正确格式化您的代码...
标签: c# entity-framework