0

I am trying to save an object of type ChangeRequest, which has multiple navigation properties. For these properties, I have set their EntityState to Unchanged to prevent Entity Framework from creating duplicates in my database. For most of these properties, their states are changed successfully and they are not persisted to the database.

When I do the following: db.Entry(changeRequest_Database_Server.Database_Server.Server).State = EntityState.Unchanged; for Server object and then again:

db.Entry(changeRequest_Server_Application.Server_Application.Server).State = EntityState.Unchanged; for the same Server object, I get the exception:

Saving or accepting changes failed because more than one entity of type 'ChangeControl.Business.Server' have the same primary key value. Ensure that explicitly set primary key values are unique...

Here is my code:

db.ChangeRequests.Add(changeRequest);

db.Entry(changeRequest.BusinessUnit).State = EntityState.Unchanged;

foreach (var changeRequest_ChangeType in changeRequest.ChangeRequest_ChangeType)
{
    db.Entry(changeRequest_ChangeType.ChangeType).State = EntityState.Unchanged;
}
foreach (var changeRequest_Attachment in changeRequest.ChangeRequest_Attachment)
{
    db.Entry(changeRequest_Attachment.Attachment).State = EntityState.Unchanged;
}
foreach (var changeRequest_Attachment in changeRequest.ChangeRequest_Attachment)
{
    db.Entry(changeRequest_Attachment.Attachment).State = EntityState.Unchanged;
}
foreach (var changeRequest_Database_Server in changeRequest.ChangeRequest_Database_Server)
{
    db.Entry(changeRequest_Database_Server.Database_Server).State = EntityState.Unchanged;
    db.Entry(changeRequest_Database_Server.Database_Server.Server).State = EntityState.Unchanged;
    db.Entry(changeRequest_Database_Server.Database_Server.Database).State = EntityState.Unchanged;
}
foreach (var changeRequest_Requirement in changeRequest.ChangeRequest_Requirement)
{
    db.Entry(changeRequest_Requirement.Requirement).State = EntityState.Unchanged;
}
foreach (var changeRequest_Server_Application in changeRequest.ChangeRequest_Server_Application)
{
    db.Entry(changeRequest_Server_Application.Server_Application).State = EntityState.Unchanged;
    db.Entry(changeRequest_Server_Application.Server_Application.Server).State = EntityState.Unchanged;
    db.Entry(changeRequest_Server_Application.Server_Application.Application).State = EntityState.Unchanged;
}

db.SaveChanges();

I have tried this and this to no avail.

How can I check for untracked objects and make sure that duplicates are not being inserted in my database?

Rai Vu
  • 1,595
  • 1
  • 20
  • 30
1fuyouinfinite
  • 1
  • 1
  • 1
  • 14
  • Try moving `db.ChangeRequests.Add(changeRequest);` after resolving unmodified entities (before `db.SaveChanges();`). – Ivan Stoev Feb 02 '18 at 08:54
  • I tried it. I get the same exception :( – 1fuyouinfinite Feb 02 '18 at 09:14
  • Then probably there is some issue with your `Server` object PK property. Try debugging and examine the PK values. Also I assume `Database_Server.Server` and `Server_Application.Server` objects with the same PK value are also one and the same instance? If they were not, the exception would be different, but it's worth checking. Which line throws the exception (can you post the exception stack trace)? – Ivan Stoev Feb 02 '18 at 09:44
  • Exception: `Attaching an entity of type 'ChangeControl.Business.Server' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.` – 1fuyouinfinite Feb 02 '18 at 10:19
  • Now this is the different exception I was talking about. So definitely `Server` is **not the same object**, but different objects with one and the same PK. It's not easy to be resolved in such graph though - you have to remap them manually in advance. Or if they are backed with explicit FK property, set that property and set the navigation property to `null` as Julie suggests. – Ivan Stoev Feb 02 '18 at 10:41
  • The remap is painful but thanks a lot. It worked. – 1fuyouinfinite Feb 02 '18 at 13:00

0 Answers0