0

I have flowing scenario:

1) entity in loaded from EF with graph of entities. (STE, 4.3.1)

2) this entity may(or may not) be modified.

3) this entity is send by WCF to server.

4) I would like to make a copy of this entity and save it to DB as new entity.

Problem is that I don't know how to set all entities to Added state.

Any idea how to do this? Thanks.

Chatumbabub
  • 1,557
  • 2
  • 18
  • 30
  • im sure there's an easier way, but the first thing that comes to mind is automapper. You could use that to map all the properties (and ignore the ID field) to a new instance of the same type. – Peanut Nov 28 '12 at 12:44
  • did you try to add it to context again after clone? – Arsen Mkrtchyan Nov 28 '12 at 12:45
  • Peanut: thanks for idea. But I'm a bit worried about entity.ChangeTracker.State. It will be still in Modified(or Unchanged) state. But I will try it. ArsenMrkt: sure.. – Chatumbabub Nov 28 '12 at 12:49
  • 1
    You could just re-add the object, [ObjectContext.AddObject](http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.addobject.aspx), this will generate a new entity key, and set the object state to added- this post could help : [Add Object vs. Attach](http://stackoverflow.com/questions/3920111/entity-framework-4-addobject-vs-attach). If your object is already attached, you could also just change the state using your objectContext (objectContext.ObjectStateManager.ChangeObjectState(your object instance,EntityState.Modified); – Mark Oreta Nov 28 '12 at 12:49

0 Answers0