2013-08-19 101 views
0

我有一組關係,我不確定要在FluentAPI中進行設置。這裏是我的蒸餾模型:EF代碼優先 - 流利的API映射

public class Customer 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public virtual ICollection<Reference> References { get; set; } 
} 
public class Reference 
{ 
    public int Id { get; set; } 
    public string Relationship { get; set; } // Brother, Father, etc 
    public virtual Customer LinkedCustomer { get; set; } 
    public virtual Customer ReferenceFor { get; set; } 
} 

ReferenceFor鏈接返回到客戶參考。 LinkedCustomer引用參考所基於的客戶。基本上,客戶可以成爲任何其他數量的客戶的參考。

我認爲這是一個Many-Many和Many-One關係集,但我不確定如何在Fluent API中指定它們。任何幫助,將不勝感激!

回答

0
modelBuilder.Entity<Customer>() 
      .HasKey(x => x.Id) 
      .HasMany(x => x.References) 

modelBuilder.Entity<References>() 
      .HasKey(x => x.Id) 
      .HasOptional(x => x.LinkedCustomer) 
      .WithMany(x => x.Customer) 

modelBuilder.Entity<References>() 
      .HasKey(x => x.Id) 
      .HasOptional(x => x.ReferenceFor) 
      .WithMany(x => x.Customer)