多个外键指向Entity Framework 4.1代码中的同一张表 [英] Multiple foreign keys pointing to same table in Entity Framework 4.1 code first

查看:104
本文介绍了多个外键指向Entity Framework 4.1代码中的同一张表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持尝试为以下数据库关系编写Entity Framework 4.1代码优先模型.

I'm stuck at trying to write the Entity Framework 4.1 code first model for the following DB relationship.

这是这种关系的视觉表现.

Here is a visual of the relationship.

dbo.[公司]可以将卖方或债务人作为公司类型.

dbo.[Companies] can have either Seller or Debtor as Company Types.

dbo.[SellerDebtors]定义了卖方公司与债务人公司之间的联系.

dbo.[SellerDebtors] defines the connection a Seller Company has with a Debtor Company.

我编写的代码基于我的原始EF 4.0 POCO模型代码.这就是我想出的-此代码不起作用.

The code i've written is based on my original EF 4.0 POCO model code. This is what I've come up with - This code does not work.

public class SellerDebtor
{
    public int SellerDebtorId { get; set; }
    public int DebtorCompanyId { get; set; }
    public int SellerCompanyId { get; set; }

    public Company DebtorCompany { get; set; }
    public Company SellerCompany { get; set; }

    public ICollection<SellerDebtorInfo> SellerDebtorInfos { get; set; }
    public ICollection<SellerDebtorFile> SellerDebtorFiles { get; set; }    
}


public class Company
{
    public int CompanyId { get; set; }
    public string CompanyType { get; set; }
    public string Name { get; set; }

    public virtual ICollection<User> Users { get; set; }
    public virtual ICollection<CompanyInfo> CompanyInfos { get; set; }
    public virtual ICollection<CompanyFile> CompanyFiles { get; set; }

    public virtual ICollection<SellerDebtor> SellerDebtorDebtorCompanies { get; set; }
    public virtual ICollection<SellerDebtor> SellerDebtorSellerCompanies { get; set; }

}

此刻,我将其视为错误:

At the moment, I'm getting this as an error:

System.Data.SqlClient.SqlException: Invalid column name 'DebtorCompany_CompanyId'.
Invalid column name 'SellerCompany_CompanyId'.
Invalid column name 'Company_CompanyId'.
Invalid column name 'Company_CompanyId1'.

理想情况下,我希望能够保持关系的命名.

Ideally, I'd like to be able to maintain the naming of the relationships.

我猜我需要设置一些属性,但是我不确定该设置什么.

I'm guessing i need to set some attributes but i'm not sure what to set.

推荐答案

EF不能通过约定确定您2个类中的哪些导航属性属于一起,并创建4个关系(另一端没有结束),而不是2个(两端均两端).当您在同一类中具有多个相同类型(在您的情况下为Company)的导航属性时,总是会发生此问题.您可以尝试通过以下方式解决此问题:

EF is not able to determine by convention which navigation properties on your 2 classes belong together and creates 4 relationships (without an end on the other side) instead of 2 (with ends on both sides). This problem occurs always when you have more than one navigation property of the same type (Company in your case) in the same class. You could try to fix this the following way:

public class SellerDebtor
{
    public int SellerDebtorId { get; set; }
    [ForeignKey("DebtorCompany")]
    public int DebtorCompanyId { get; set; }
    [ForeignKey("SellerCompany")]
    public int SellerCompanyId { get; set; }

    [InverseProperty("SellerDebtorDebtorCompanies")]
    public Company DebtorCompany { get; set; }
    [InverseProperty("SellerDebtorSellerCompanies")]
    public Company SellerCompany { get; set; }

    public ICollection<SellerDebtorInfo> SellerDebtorInfos { get; set; }
    public ICollection<SellerDebtorFile> SellerDebtorFiles { get; set; }    
}

[InverseProperty(...)]在关系的另一端定义了导航属性,并明确告诉EF关系中哪些导航属性对属于同一对.

[InverseProperty(...)] defines the navigation property on the other end of the relationship and it tells EF explicitely which pairs of navigation properties belong together in a relationship.

这篇关于多个外键指向Entity Framework 4.1代码中的同一张表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆