2011-05-16 3 views
17

다음 DB 관계에 대한 Entity Framework 4.1 코드 첫 번째 모델을 작성하는 데 어려움이 있습니다.Entity Framework 4.1 코드에서 동일한 테이블을 가리키는 여러 개의 외래 키

다음은 관계를 시각적으로 나타낸 것입니다. enter image description here

dbo. [회사]는 판매자 또는 채무자 중 하나를 회사 유형으로 사용할 수 있습니다.

dbo [SellerDebtors]는 판매자 회사가 Debtor Company와의 연결을 정의합니다.

작성한 코드는 원래 EF 4.0 POCO 모델 코드를 기반으로 작성되었습니다. 이것이 내가 생각해 낸 것입니다.이 코드는 작동하지 않습니다. 순간

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

} 

, 내가 오류로 받고 있어요 :

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

를 이상적으로, 나는 관계의 이름을 유지할 수 있도록하고 싶습니다.

나는 몇 가지 속성을 설정해야한다고 추측하지만 설정해야 할 항목이 확실하지 않습니다.

+0

[엔티티 프레임 워크 코드 첫 번째 - 두 개의 외래 키 같은 테이블]의 사용 가능한 복제 (http://stackoverflow.com/questions/5559043/entity-framework-code-first-two- foreign-keys-from-same-table) –

답변

18

EF는 2 개의 클래스에 대한 탐색 속성이 함께 속하는지를 규칙적으로 결정할 수 없으며 (양쪽에 끝이있는) 2 대신에 (네쪽에 끝이없는) 4 개의 관계를 만듭니다. 이 문제는 동일한 클래스의 동일한 유형 (사용자의 경우 Company)의 탐색 속성이 둘 이상있을 때 항상 발생합니다. 이 다음과 같은 방법으로 해결을 시도 할 수 있습니다 :

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를 알려줍니다.

3

이 블로그에는 Fluent API 구성을 사용한 예가 있습니다.

Multiple foreign keys within same table using CodeFirst Entity Framework and Fluent API

modelBuilder.Entity<Branch>().HasOptional(b => b.PrimaryContact)   
      .WithMany(a => a.PrimaryContactFor).HasForeignKey(b=>b.PrimaryContactID); 
+0

정말 기쁘게 게시했습니다. 오늘 Fluent API 예제가 필요했습니다. – Brandon

+0

IMHO 가장 좋은 답변입니다. 유창한 API는 맑은 날만큼 명확합니다. – alex440

관련 문제