I'm trying to do a deep clone of some entities. The approach mentioned in this article looks good but I run into errors. It suggests using AsNoTracking() to retrieve the entity and then to reinsert it into the context where it will cause an insert as it looks like a new object.
Here's my code:
Using ctxt As New ProductionDataEntities
Dim grade = ctxt.Grades.Include(Function(g) g.GradeWidths).AsNoTracking.First
ctxt.Grades.AddObject(grade)
ctxt.SaveChanges()
End Using
But when I run it I get:
An object with the same key already exists in the ObjectStateManager. The existing object is in the Modified state. An object can only be added to the ObjectStateManager again if it is in the added state.
And when I modify grade.Name it's EntityState changes to modified implying that it is being Tracked.
I'm using EF5 Db-First.
Alternatively I've tried cloning by detaching the grade and then reinserting it, this works but the GradeWidths don't get copied. As soon as I call detach the gradewidth count goes from 2 to 0.
Questions:
- Any idea why AsNoTracking isn't working and what I can do to fix this?
- Alternatively is there another approach someone can recommend for simple deep cloning?
Thanks.
---- additional info ----
I have 5 1-to-many relationships and eventually I'll want to clone from the top level all the way down. But I'm simplifying it to just look at the lowest level.
- 1 Grade to many GradeWidths