0

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

现在我用 LookupT 添加了另一个名为 InvoiceStatus 和 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;}
}
4

1 回答 1

2

So I figured out the issue. Seems like when EF creates the navigational properties, it does this in order of FK relation names i.e. if I have two FK relations with name RelationA and RelationB, then the navigational property for RelationA will be created first and then for RelationB. In my scenario, the relation names for my type and status were 'FK_InvoiceT_LookupT_InvoiceTypeLkpId' and 'FK_InvoiceT_LookupT_InvoiceStatusLkpId' (which SQL designer generates automatically) respectively and EF first generated the navigational property for the relation that was added later and it messed up my ordering. So all I had to do was to update relation names in order in which they were added and everything started working fine.

于 2018-12-20T10:42:35.293 回答