use*_*346 5 c# entity-framework code-first ef-code-first
当有 2 个导航时,我在 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; }
}
Run Code Online (Sandbox Code Playgroud)
这个特定类的场景反映了以下内容: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; }
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
693 次 |
最近记录: |