当有 2 个导航时,我遇到两个实体之间的连接问题。
具体来说,我有以下课程:
public class TableA
{
public TableA()
{
ListBs = new List<TableB>();
}
[Key]
public int Id { get; set; }
public TableB MainB { get; set; }
public virtual ICollection<TableB> ListBs { get; set; }
}
public class TableB
{
[Key]
public int Id { get; set; }
public virtual TableA refA { get; set; }
[Required]
public string Text { get; set; }
}
这个特定类的场景反映了以下内容:
TableA 有 TableB 对象的列表
并且还有 1 个主 TableB 对象(当然也在列表中)。
此外,TableB 对象实际上可能没有对 TableA 的引用
抓取工作。
但是当我尝试插入新项目时,出现以下异常:
无法确定相关操作的有效顺序。由于外键约束、模型要求或存储生成的值,可能会存在依赖关系。
知道我哪里出了什么问题吗?
使用此代码:
public class TableA
{
public TableA()
{
ListBs = new List<TableB>();
}
[Key]
public int Id { get; set; }
public int TableB_Id { get; set; }
[InverseProperty("TableA_Mains")]
[ForeignKey("TableB_Id")]
public TableB MainB { get; set; }
[InverseProperty("refA")]
public virtual ICollection<TableB> ListBs { get; set; }
}
public class TableB
{
[Key]
public int Id { get; set; }
public int TableA_Id { get; set; }
[Foreignkey("TableA_Id")]
[InverseProperty("ListBs")]
public virtual TableA refA { get; set; }
[Required]
public string Text { get; set; }
[InverseProperty("MainB")]
public virtual ICollection<TableA> TableA_Mains { get; set; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)