我对流畅的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; }
}
公司分类
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; }
}
在 Code First 中映射多对多关系的最佳方法是:
public class Classification
{
[Key]
[DisplayName("Id")]
public int id { get; set; }
...
public virtual ICollection<Company> Companies{ get; set; }
}
public class Company
{
public int Id { get; set; }
...
public virtual ICollection<Classification> Classifications { get; set; }
}
默认情况下,代码优先将创建第三个连接表,ClassificationCompanies
该表将包含两个表的PK,即。 ClassificationId
CompanyId
.
如果需要指定联结表和 FK 列的名称,则可以使用 Fluent API 配置覆盖上下文中OnModelCreating
方法的关系:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Classification>()
.HasMany<Company>(s => s.Companies)
.WithMany(c => c.Classifications)
.Map(cs =>
{
cs.MapLeftKey("ClassificationId");
cs.MapRightKey("CompanyId");
cs.ToTable("ClassificationCourses");
});
}
另一种方法是创建一个表示联结表的实体(就像您尝试的那样)并使用 Classification
和 Company
建立两个一对多关系,但如果不需要向联结表添加额外的列,建议使用第一个变体。如果遵循此变体,则表示联结表的实体将如下所示:
public class CompanyPkdClassification
{
public int CompanyId { get; set; }
public int PkdClassyficationId { get; set; }
public virtual Company Company { get; set; }
public virtual Classification Classification { get; set; }
}
和配置:
modelBuilder.Entity<CompanyPkdClassification>().HasKey(c => new { c.PkdClassyficationId, c.CompanyId });
modelBuilder.Entity<Company>()
.HasMany(c => c.companyPkdClassification)
.WithRequired(cc=>Company)
.HasForeignKey(c => c.CompanyId);
modelBuilder.Entity<PkdClassification>()
.HasMany(c => c.companyPkdClassification)
.WithRequired(cc=>Classification)
.HasForeignKey(c => c.PkdClassyficationId);}
你能显示你用来获取值的查询吗?我认为这与延迟加载的对象有关。如果在请求导航属性之前执行 ToList(),则这些值将为 null。您必须通过在查询中添加此值来强制包含该值:
.Include(x => x.Company)
依此类推,对于您想要的每个对象,或者在对象仍处于活动状态时请求值。 即在你执行 ToList() 之前;这里更详细地解释:https://msdn.microsoft.com/en-gb/library/vstudio/bb738633%28v=vs.100%29.aspx
根据您在下面的评论,FirstOrDefault() 将执行查询,因此在此之后无法再填充导航属性。我会尝试这个(提供dbContext.company是一组公司对象):
添加用于系统.数据.实体;
public Company getCompany(int id) {
Company data = dbContext.company.Include(x => x.companyPkdClassification .Select(y => y.company)).Include(x => x.companyPkdClassification .Select(y => y.pkdClassification)).Where(i => i.id == id).FirstOrDefault();
return data;
}