简体   繁体   中英

Unable to determine the principal end of the relationship, Multiple added entities may have the same primary key

Sorry, first let me say I did try to search this problem online and there are some but none specifically related to my scenario. I spent couple of hours and could not figure out.

I post my code below, stripping out all irrelevant information

First, here are my classes

public class StudentDm
{
    public int Id { get; set; }
    public virtual List<StudentParentDm> StudentParents { get; set; }

    // other properties ...
}

// constructs a many to many relationship with some additional info in this model
public class StudentParentDm : EntityBaseDm
{
    public int Id { get; set; }
    public int StudentId { get; set; }
    public virtual StudentDm Student { get; set; }
    public int ParentId { get; set; }
    public virtual ParentDm Parent { get; set; }

    // other properties ...
}

public class ParentDm
{
    public int Id { get; set; }

    // other properties ...
}

Mappings:

public StudentMap()
{
    HasMany(m => m.StudentParents).WithRequired().HasForeignKey(m => m.StudentId).WillCascadeOnDelete(false);
}

public StudentParentMap()
{
    HasRequired(m => m.Student).WithMany().HasForeignKey(m => m.StudentId).WillCascadeOnDelete(false);
    HasRequired(m => m.Parent).WithMany().HasForeignKey(m => m.ParentId).WillCascadeOnDelete(false);
}

public ParentMap()
{
    HasMany(m => m.StudentParents).WithRequired().HasForeignKey(m => m.ParentId).WillCascadeOnDelete(false);
}

Then the code, here I am trying to create multiple new StudentParents, each with its own new Parent, to a student.

foreach (StudentParentDm studentParent in studentParents) // foreach new studentParent
{
    StudentParentDm trackedStudentParent;
    if (studentParent.Id == 0)
    {
        trackedStudentParent = new StudentParentDm
        {
            Parent = new ParentDm()
        };

        // map from studentParent to trackedStudentParent, including the Parent
        // ...

        trackedStudent.StudentParents.Add(trackedStudentParent);
    } else 
    {
        // unimportant
    }
}

unitOfWork.Commit() // blows up with error message

Then I get this message:

Unable to determine the principal end of the 'Cobro.BusinessObjects.DatabaseContextServices.ParentDm_StudentParents' relationship. Multiple added entities may have the same primary key.

This only happens when I try to add more than 1 StudentParent at a time. I am not sure why the number of StudentParents would matter? I think I have the relationships set up correctly.

It also works with adding multiple StudentGrades, but the difference is that StudentGrade model is flat (does not have any child like StudentParent has a Parent)

Since nobody answered. Ill post what I discovered in case it can help anyone.

In StudentParentMap, add

m => m.Parent

such that

HasMany(m => m.StudentParents).WithRequired(m => m.Parent).HasForeignKey(m => m.ParentId).WillCascadeOnDelete(false);

Not sure why this mattered.. the mapping seemed sufficient enough such that the code-first generated database relationship did not change with this new addition. However, it was needed for EF to figure out how to configure FK during a multiple records adding transaction. It was pretty subtle for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM