您必须在模型中公开CFFPartDisp为实体类。您不能将其用作问题中的 Fluent 映射之间CFFPart的链接表。和Disp之间的关系不是多对多的关系(在严格的 EF 意义上)。相反,您必须创建两个作为中间实体的一对多关系。然后,您可以将您与该中间实体之间的关系链接为第三个关系。CFFPartDispCFFPartDispCFFPartDispExpert
该CFFPartDisp实体可能如下所示:
public class CFFPartDisp
{
public int ID { get; set; }
public int CFFPartID { get; set; }
public CFFPart CFFPart { get; set; }
public int DispID { get; set; }
public Disp Disp { get; set; }
public ICollection<Expert> Experts { get; set; }
}
CFFPart和实体需要引用的Disp集合CFFPartDisp:
public class CFFPart
{
public int ID { get; set; }
public ICollection<CFFPartDisp> CFFPartDisps { get; set; }
}
public class Disp
{
public int ID { get; set; }
public ICollection<CFFPartDisp> CFFPartDisps { get; set; }
}
并且还Expert需要一个集合来建立和CFFPartDisp之间的多对多关系:CFFPartDispExpert
public class Expert
{
public int ID { get; set; }
public ICollection<CFFPartDisp> CFFPartDisps { get; set; }
}
使用这些实体,您可以创建三个关系:
modelBuilder.Entity<CFFPartDisp>()
.HasRequired(cpd => cpd.CFFPart)
.WithMany(cp => cp.CFFPartDisps)
.HasForeignKey(cpd => cpd.CFFPartID);
modelBuilder.Entity<CFFPartDisp>()
.HasRequired(cpd => cpd.Disp)
.WithMany(cp => cp.CFFPartDisps)
.HasForeignKey(cpd => cpd.DispID);
modelBuilder.Entity<CFFPartDisp>()
.HasMany(cpd => cpd.Experts)
.WithMany(e => e.CFFPartDisps)
.Map(m =>
{
m.MapLeftKey("CFFPartDispID");
m.MapRightKey("ExpertID");
m.ToTable("CFFpartDisPExpert");
});