2013-08-19 2 views
0

У меня есть набор отношений, которые я не уверен в настройке в FluentAPI. Вот моя дистиллированная модель:EF Code-First - Fluent API Mapping

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

СсылкаДля ссылок на ссылки в Customer. 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) 
Смежные вопросы