实体框架交换导航属性

Ars*_*bal 2 .net asp.net entity-framework edmx edmx-designer

我将EF与DB first方法一起使用。更新EDMX文件时遇到问题。假设我有一个表InvoiceT和另一个表LookupT。现在,在InvoiceT中,我有一列InvoiceType与LookupT具有FK关系。现在,实体在InvoiceT类中创建了名为LookupT的导航属性。到目前为止,它很好。

现在,我添加了名为InvoiceStatus的另一列和带有LookupT的FK。现在,实体创建了另一个名为LookupT1的导航属性。但是这里的问题是,第一个导航属性LookupT没有指向其原始数据,即InvoiceType。相反,它现在指向InvoiceStatus数据。有人可以向我解释为什么它会这样,我该怎么办?

public class InvoiceT
{
    public int InvoiceId {get;set;}
    public int InvoiceStatusLkpId {get;set;}
    public int InvoiceTypeLkpId {get;set;}
    public virtual LookupT LookupT {get;set;}    // Previously pointing to Type. Now to Status.
    public virtual LookupT LookupT1 {get;set;}    // Pointing to Type

}

public class LookupT
{
    public int LookupId {get;set;}
    public string LookupValue {get;set}
    public virtual ICollection<InvoiceT> InvoiceT {get;set;}
    public virtual ICollection<InvoiceT> InvoiceT1 {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

Ars*_*bal 5

所以我想出了问题。好像当EF创建导航属性时,它是按照FK关系名称的顺序执行的,即如果我有两个名称分别为RelationA和RelationB的FK关系,则将首先为RelationA创建导航属性,然后为RelationB创建导航属性。在我的场景中,我的类型和状态的关系名称分别是“ FK_InvoiceT_LookupT_InvoiceTypeLkpId”和“ FK_InvoiceT_LookupT_InvoiceStatusLkpId”(SQL设计器会自动生成),并且EF首先为该关系生成了导航属性,该属性随后添加并弄乱了我的订购。因此,我要做的就是更新关系名称,以便按顺序添加关系名称,一切开始正常进行。