问题
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; }
}
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?
回答1:
use this Code :
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; }
}
来源:https://stackoverflow.com/questions/21110614/more-than-one-navigation-to-the-same-entity