我对流利的 api 和实体框架(多对多关系)有疑问。当我尝试从数据库中获取数据时,对象Company
和pkdClassification
. 价值CompanyId
和PkdClassyficationId
回报正确。
语境
//in contrstructor set
// this.Configuration.LazyLoadingEnabled = true;
.Entity<CompanyPkdClassification>()
(c => new { c.PkdClassyficationId, c.CompanyId });
modelBuilder.Entity<Company>()
.HasMany(c => c.companyPkdClassification)
.WithRequired()
.HasForeignKey(c => c.CompanyId);
modelBuilder.Entity<PkdClassification>()
.HasMany(c => c.companyPkdClassification)
.WithRequired()
.HasForeignKey(c => c.PkdClassyficationId);}
pkd分类
public class PkdClassification
{
[Key]
[DisplayName("Id")]
public int id { get; set; }
...
public virtual ICollection<CompanyPkdClassification> companyPkdClassification { get; set; }
}
公司PKD分类
public class CompanyPkdClassification
{
[ForeignKey("company")]
public int CompanyId { get; set; }
[ForeignKey("pkdClassification")]
public int PkdClassyficationId { get; set; }
public virtual ICollection<Company> company { get; set; }
public virtual ICollection<PkdClassification> pkdClassification { get; set; }
}
最后一家公司
public class Company : baseModel
{
...
virtual ICollection<CompanyPkdClassification> companyPkdClassification { get; set; }
}