简体   繁体   中英

An entity object cannot be referenced by multiple instances of IEntityChangeTracker. On multiple entries

I have a work object:

public class Work{
public int WorkId { get; set; }
public virtual Work RelatedWork { get; set; }
public virtual ICollection<Work> RelatedMultipleWorks { get; set; }
}

I'm generating an Work object like this :

Work myWork = new Work();

work mywork2 = new Work();
work mywork3 = new Work();

myWork.RelatedMultipleWorks.add(mywork2);
myWork.RelatedMultipleWorks.add(mywork3);

On DbSaveChanges , I get this exception :

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

What is wrong with the code? How can I fix this? Thanks.

I get the exception at this line :

_db.Works.Add(mywork);

Here is my DbSaveChanges Line :

  _db.Works.Add(_myWork);
  _db.SaveChanges();
  result.Success= true;
  _provider.AddOrUpdate(mywork);

Note : In the actual code, Work entity has a lot more entities. Like hundreds, so I can't post actual code.

EDIT : When I try to add myWork2 or myWork3 using _db.Works.Add(_myWork2); , I still get the same error. Is this error because of a missing entity or something like that?

Ultimately, this is the same problem mentioned here: How can I use EF to add multiple child entities to an object when the child has an identity key?

If you have a auto generated PK and attempt to do an Add after doing a previous udpate/delete on the same context, it will crash because you end up with the same PK s for multiple objects at the same time.

To fix this, you must either set all those WorkIds (I assume this is the PK ) to -1, -2, -3 and their proper FK references to those new numbers
OR
You must add all new entities first before you do any updates/deletes since EF will realize that those IDs will be set in the future.

The problem exists here actually:

Work myWork = new Work();    
work mywork2 = new Work();
work mywork3 = new Work();

You are creating a data context in the public constructor of each class as this must be what is building the work class, you are then trying to add it and build multiple relations to each objects in your collection using multiple contexts instantiated of the same Entity object. This is the complaint. The fix is to create a new constructor that takes a context that was created outside of the classes and use it as a parameter to instantiate each class. They should look like this:

work myWork = new Work(mySingleContext);
work myWork2 = new Work(mySingleContext);
work myWork3 = new Work(mySingleContext);

This should satisfy the requirements, however be aware that if you are doing the same thing with other items and adding a context to another item instantiated from the same Entity item you will still error. However, now when you call the .Add() method the items will be sharing the same Entity instance and the error should not be raised.

Add tags to your object on the ID property

 [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
 [Key]

Maybe you could try:

myWork.RelatedMultipleWorks.addRange(new List<Work>{ mywork1, mywork2 });

Second try on this one:

What happens if you remove the virtual keyword from the RelatedMultipleWorks member of the Work object?

Third run:

WorkId is the primary key? Maybe it needs a value?

Also seems like the child objects would need to be added to the database first - the primary key is a reference to an existing object.

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