2016-10-20 7 views
1

私は2つのエンティティ、すなわち従業員と会社を持っています。どちらも1つまたは複数のアドレスを持つことができます。 Guidは常にユニークなので、私はEmployeeとCompanyのGuidをAddressの外部キーとして使いたいと思っています。
従業員の住所に複数のエントリがあり、Guid of EmployeeがGuidフィールドの住所になります。
同じように、会社のアドレスも複数あることがあります。そして、会社の指導者は、指導の指導者になります。流暢なAPI - 1 2多くの関係

あなたは私があなたの問題を理解している場合はわからない

public class Employee 
{ 
    public int EmployeeId; 
    public Guid Guid; 
    . 
    . 
    . 
    public ICollection<Address> Addresses; 
} 

public class Company 
{ 
    public int CompanyId; 
    public Guid Guid; 
    . 
    . 
    . 
    public ICollection<Address> Addresses; 
} 

public class Address 
{ 
    public int AddressId 
    public Guid Guid; // Guid from Employee or Company 
    . 
    . 
    . // Should here be Navigation to Employee/Company as well? 


} 

答えて

2

流暢APIを使用して/ wの従業員、住所、当社-アドレスbの関係を設定する方法私を助けてください。あなたは、2つの単純な1をしたいですか:Nこのような場合は、これらの?:

Emplyee 1:N Adress 
Company 1:N Adress 

のような関係は、あなたがこのモデルを持っている必要があります:あなたは

public class Employee 
{ 
    public int EmployeeId { get; set; }; 
    // ... 
    public virutal ICollection<Address> Addresses { get; set; }; 
} 

public class Company 
{ 
    public int CompanyId { get; set; }; 
    // ... 
    public ICollection<Address> Addresses { get; set; }; 
} 

public class Address 
{ 
    public int AddressId { get; set; }; 
    public int? EmployeeId { get; set; }; 
    public int? CompanyId { get; set; }; 
    // ... 
    public virtual Employee Employee { get; set; }; 
    public virtual Company Company { get; set; }; 
} 
1

セットアップ

public class Employee 
{ 
    //no need of following line. just use the GUID as Employee id 
    //public int EmployeeId; 
    public Guid EmployeeId; 
    . 
    . 
    . 
    public ICollection<Address> Addresses; 
} 

public class Company 
{ 
    public int CompanyId;//no need of this line, use guid as company id 
    public Guid CompanyId; 
    . 
    . 
    . 
    public ICollection<Address> Addresses; 
} 

public class Address 
{ 
    public int AddressId 
    public Guid OwnerId; // Guid from Employee or Company 
    . 
    . 
    //don't add Navigation to Employee/Company 


} 

のようなエンティティ流暢なAPIでslaumaが提案したのはhereです。

modelBuilder.Entity<Company>() 
.HasMany(c => c.Addresses) 
.WithRequired() 
.HasForeignKey(a => a.OwnerId); 

modelBuilder.Entity<Employee>() 
.HasMany(c => c.Addresses) 
.WithRequired() 
.HasForeignKey(a => a.OwnerId); 
関連する問題