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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…