more than one navigation to the same entity

╄→гoц情女王★ 提交于 2019-12-01 05:34:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!