我试图流利地配置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; }
}
上述代码将导致错误:
无效的Exception:无法确定关系 由导航物业"公司" 'List&lt; companyownership&gt;'。要么手动配置关系, 或使用" [未绘制]]属性或使用 " enmodeLcreating"中的" entityTypebuilder.ignore"。
我能否获得一些输入,以了解为什么上述设置还不够?
谢谢,
nvm,
事实证明,我忘了调用配置(...(。
现在工作正常。