您必须将CFFPartDisp
作为模型中的实体类公开。您不能将它用作CFFPart
和Disp
之间的链接表,并在您的问题中使用Fluent映射。 CFFPart
和Disp
之间的关系不是多对多的关系(在严格的EF意义上)。相反,您必须创建两个与CFFPartDisp
作为中间实体的一对多关系。然后,您可以将您与CFFPartDisp
和Expert
之间的关系作为第三关系链接到此中间实体。
的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
集合以及建立CFFPartDisp
和Expert
之间的多对多关系:
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");
});
你能写出更多的细节?可能是显示数据库图。你的问题是什么? –
@KirillBestemyanov - 我从Code First的两个表(CFFPart和Disp)创建一个组合表(CFFpartDisp)。数据库当前使用组合表的ID(主键)与专家建立关系。我不知道如何使用CodeFirst将组合表ID与专家ID进行映射?任何建议,将不胜感激。 – akki