多于一个的导航,同一个实体 [英] more than one navigation to the same entity
问题描述
我有2实体时,有2导航之间的连接问题
I have a problem with connection between 2 entities when there is 2 navigations.
是具体的,我有以下类:
to be specific, I have the following classes:
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; }
}
这个特殊类的场景反映了以下几点:
表A具有表b的对象的列表
和还具有1个主表b对象(即课程列表中的太)。
也是一个表B对象不能actualy有表A
The scenario of this particular classes reflects the following: TableA has a list of TableB objects and also has 1 main TableB object(that is of course in the list too). Also a TableB object may not actualy have a reference to TableA
取作品的引用。
,但是当我尝试插入新项目出现以下异常:
the fetching works. but when I try to insert new items I get the following exception:
无法确定相关的操作有效的排序。依赖可能存在由于外键约束,模型要求,或存储生成的值。
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
任何想法,我有什么错?
Any idea where I got anything wrong?
推荐答案
使用此代码:
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; }
}
这篇关于多于一个的导航,同一个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!