简体   繁体   English

多个导航到同一个实体

[英]more than one navigation to the same entity

I have a problem with connection between 2 entities when there is 2 navigations. 当有2个导航时,我有2个实体之间的连接问题。

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). 此特定类的方案反映了以下内容:TableA具有TableB对象的列表,并且还具有1个主TableB对象(当然也在列表中)。 Also a TableB object may not actualy have a reference to TableA 另外,TableB对象可能实际上不具有对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? 知道我哪里弄错了吗?

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; }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM