开发者

Entity Framework 4 - Difficulty understanding how cloning an entity works

Hi I am trying to make a clone of an EF entity and then reinsert it into the context for saving back to a DB as a new row. Later when I have solved that, I would like to expand this to clone a full entity graph and save it.

I have tried two code samples:

http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/a967b44b-c85c-4afd-a499-f6ff604e2139

which both seem to be working. I think the problem is my understanding of what is taking place. As I understand it, the clone process makes a detached clone of the target o开发者_Python百科bject, which I should be able to Attach or AddObject back to the context, and have the context save it to the DB where it will be automatically assigned a unique PK ID.

The main thing confusing me is that when I clone my entity object and then look at the properties of the clone in debug mode, it seems like its navigation collections still apparently point to valid entities that the copied object points to. However the EntityState is Detached. Is this normal? I assumed that the clone object's navigation properties are pointing to the same objects as in the cloned object's graph. But the clone objects are 'Detached' and the cloned objects are 'Unchanged'.

I'm sooo confused... any ideas?


Deep copy (clone of the whole object graph) must result into two equivalent object graphs. Original graph will be still attached to context. Second graph will be detached from object context. Depending on depth of cloning process there will be no cross references between graphs. It means that if you really clone everything, your navigation properties in sencond graph will never point to attached entities (from original graph).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜