Entity Framework core many to many not inserting

entity-framework entity-framework-core

Question

I am using EF7 and have a scenario which needs a many to many relationship.

I have a ParticipantSIR entity and a ParticipantAssessmentReport entity. There is a link table ParticipantSIRAssessmentReport between them.

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);
        });

This appears to be the way this needs to be setup with EF core including the third entity. I got some of the information from. http://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many When I insert data the 2 outside entities get populated but not the link table.

Since there are no navigation properties between the ParticipantSIR and ParticipantAssessmentReport then I'm not sure how to add the linked data.

  _db.ParticipantAssessmentReport.Add(participantAssessmentReport);
            foreach (var sir in participantSirs)
            {
                _db.ParticipantSIR.Add(sir);
            }
            _db.SaveChanges();
1
2
4/29/2016 2:33:33 AM

Accepted Answer

Assuming we're talking about EF Core 1.0rc1 it looks like you have created your model correctly (except the virtual keyword doesn't do anything yet as lazy loading hasn't been implemented).

As many-to-many hasn't been implemented yet as of 1.0rc1 you need to do some extra work. See https://github.com/aspnet/EntityFramework/issues/1368#issuecomment-180066124 for the classic blog Post, Tag, PostTag example code.

In your case you need to explictly add to ParticipantSIRAssessmentReport, something like this:

    var participantSIRAssessmentReport = new ParticipantSIRAssessmentReport {ParticipantSIR = participantSIR, ParticipantAssessmentReport = participantAssessmentReport };  
    _db.ParticipantSIRAssessmentReport.Add(participantSIRAssessmentReport);  
    _db.SaveChanges();
1
4/29/2016 4:09:34 PM

Popular Answer

To map Many-To-Many relationships in EF you need to add the following to your DbContext's OnModelCreating() method:

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

From here the relationship will be handled using the Collections inside each of the classes.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow