Entity framework 实体框架核心多对多不插入
我正在使用EF7,有一个场景需要多对多关系 我有一个ParticipantSIR实体和一个ParticipantAssessmentReport实体。它们之间有一个链接表ParticipantSIRAssessmentReportEntity framework 实体框架核心多对多不插入,entity-framework,entity-framework-core,Entity Framework,Entity Framework Core,我正在使用EF7,有一个场景需要多对多关系 我有一个ParticipantSIR实体和一个ParticipantAssessmentReport实体。它们之间有一个链接表ParticipantSIRAssessmentReport public class ParticipantSIR { public int ParticipantSIRID { get; set; } public virtual ICollection<ParticipantSIRAssessmen
public class ParticipantSIR
{
public int ParticipantSIRID { get; set; }
public virtual ICollection<ParticipantSIRAssessmentReport> ParticipantSIRAssessmentReport { get; set; }
public virtual Participant Participant { get; set; }
}
public class ParticipantAssessmentReport
{
public int ParticipantAssessmentReportID { get; set; }
public virtual ICollection<ParticipantSIRAssessmentReport> ParticipantSIRAssessmentReport { get; set; }
}
public partial class ParticipantSIRAssessmentReport
{
public int ParticipantSIRID { get; set; }
public int ParticipantAssessmentReportID { get; set; }
public virtual ParticipantAssessmentReport ParticipantAssessmentReport { get; set; }
public virtual ParticipantSIR ParticipantSIR { get; set; }
}
modelBuilder.Entity<ParticipantSIRAssessmentReport>(entity =>
{
entity.HasKey(e => new { e.ParticipantSIRID, e.ParticipantAssessmentReportID });
entity.HasOne(d => d.ParticipantAssessmentReport).WithMany(p => p.ParticipantSIRAssessmentReport).HasForeignKey(d => d.ParticipantAssessmentReportID).OnDelete(DeleteBehavior.Restrict);
entity.HasOne(d => d.ParticipantSIR).WithMany(p => p.ParticipantSIRAssessmentReport).HasForeignKey(d => d.ParticipantSIRID).OnDelete(DeleteBehavior.Restrict);
});
要在EF中映射多对多关系,需要将以下内容添加到
DbContext
的OnModelCreating()方法中:
modelBuilder.Entity<ParticipantSIR>()
.HasMany(e => e.ParticipantSIRAssessmentReport)
.WithMany(e => e.ParticipantSIR)
.Map(e => e.ToTable("ParticipantSIRAssessmentReport") //Name of the linking table
.MapLeftKey("ParticipantSIRId") //Name of the Left column
.MapRightKey("ParticipantSIRAssessmentReportId")); //Name of the right column
modelBuilder.Entity()
.HasMany(e=>e.ParticipantSIRAssessmentReport)
.有许多(e=>e.参与者先生)
.Map(e=>e.ToTable(“ParticipantSIRAssessmentReport”)//链接表的名称
.MapLeftKey(“ParticipantSIRId”)//左栏的名称
.MapRightKey(“参与者SirAssessmentReportId”)//右列的名称
从这里开始,将使用每个类中的集合来处理关系。假设我们讨论的是EF Core 1.0rc1,看起来您已经正确地创建了模型(除了virtual关键字没有做任何事情,因为尚未实现延迟加载)
从1.0rc1开始,多对多还没有实现,您需要做一些额外的工作。请参阅,以获取经典的博客文章、标记、PostTag示例代码
在您的案例中,您需要明确地将以下内容添加到参与者评估报告中:
var participantSIRAssessmentReport = new ParticipantSIRAssessmentReport {ParticipantSIR = participantSIR, ParticipantAssessmentReport = participantAssessmentReport };
_db.ParticipantSIRAssessmentReport.Add(participantSIRAssessmentReport);
_db.SaveChanges();
var participantSIRAssessmentReport = new ParticipantSIRAssessmentReport {ParticipantSIR = participantSIR, ParticipantAssessmentReport = participantAssessmentReport };
_db.ParticipantSIRAssessmentReport.Add(participantSIRAssessmentReport);
_db.SaveChanges();