0

我有一组关系,我不确定要在 FluentAPI 中建立。这是我的蒸馏模型:

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 引用引用所基于的客户。基本上,客户可以是任何其他数量的客户的参考。

我认为这是一组多对多和多对一的关系,但我不确定如何在 Fluent API 中指定它们。任何帮助,将不胜感激!

4

1 回答 1

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)
于 2013-08-19T20:43:50.340 回答