【问题标题】:EF Core - Fluent API many-to-many self referenceEF Core - Fluent API 多对多自引用
【发布时间】:2018-03-28 21:07:45
【问题描述】:

我正在尝试流畅地配置 2 个类。

public class Company 
{
    [Key]
    public int Id {get; set; }
    public string Name { get; set; }
    public List<CompanyOwnership> OwnedBy { get; set; }
}

public class CompanyOwnership 
{
    public static void Configure(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<CompanyOwnership>()
            .HasOne(cpo => cpo.OwnedCompany)
            .WithMany(cp => cp.OwnedBy)
            .HasForeignKey(cpo => cpo.OwnedCompanyId);    
        modelBuilder.Entity<CompanyOwnership>()
            .HasOne(cpo => cpo.OwningCompany)
            .WithMany()
            .HasForeignKey(cpo => cpo.OwningCompanyId);
    }

    [Key]
    public int Id {get; set; }
    public int OwnedCompanyId { get; set; }
    public Company OwnedCompany { get; set; }
    public int OwningCompanyId { get; set; }
    public Company OwningCompany { get; set; }
    public decimal Percentage { get; set; }
}

以上代码会报错:

InvalidOperationException:无法确定关系 由类型的导航属性“Company.OwnedBy”表示 '列表'。要么手动配置关系, 或使用“[NotMapped]”属性或使用忽略此属性 “OnModelCreating”中的“EntityTypeBuilder.Ignore”。

我能否就上述设置不够的原因提供一些意见?

谢谢,

【问题讨论】:

    标签: ef-fluent-api ef-core-2.0


    【解决方案1】:

    NVM,

    原来我忘记调用 Configure(...)。

    现在一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 2018-06-21
      • 1970-01-01
      • 2012-09-22
      相关资源
      最近更新 更多