我在实体框架6中处理父子关系时遇到问题,其中父项有一个子项列表,而一个子项是最喜欢的子项。执行实体添加时,EF抛出该错误:
错误! 无法确定相关操作的有效顺序。依赖关系可能是由于外键约束,模型要求或存储生成的值引起的。
示例:
figlio.animate({
position: posInizPadre,
style: {
backgroundColor: 'green'
}
}, {
duration: 2000
});
padre.animate({
position: posInizFiglio,
style: {
backgroundColor: 'red'
}
}, {
duration: 2000
});
它不起作用,或者:
public class Child
{
public int ChildID { get; set; }
public string ChildName { get; set; }
[ForeignKey("Parent")]
public int ParentRefId { get; set; }
public Parent Parent { get; set; }
}
public class Parent
{
public int ParentId { get; set; }
public string ParentName { get; set; }
public int FavoriteChildId {get;set;}
[ForeignKey("FavoriteChildId")]
public Child FavoriteChild {get;set;}
[ForeignKey("ParentRefId")]
public ICollection<Child> Children { get; set; }
}
答案 0 :(得分:0)
对于这种配置,我更喜欢fluent api,但是您可以尝试InverseProperty annotation。
public class Child
{
public int ChildID { get; set; }
public string ChildName { get; set; }
// EF will make FK by convention
public int ParentId { get; set; }
public Parent Parent { get; set; }
public int? FavoriteOfParentId { get; set; }
public Parent FavoriteOfParent { get; set; }
}
public class Parent
{
public int ParentId { get; set; }
public string ParentName { get; set; }
public int FavoriteChildId {get;set;}
[InverseProperty("FavoriteOfParent")]
public Child FavoriteChild {get;set;}
[InverseProperty("Parent")]
public ICollection<Child> Children { get; set; }
}