开发者

Entity Framework Attach Exception After Clone

开发者 https://www.devze.com 2023-01-22 01:14 出处:网络
After trying several options of having a decent mechanism which allows to use ObservableCollections with the option to dynmically having them updated using an Edit window and binding, without having t

After trying several options of having a decent mechanism which allows to use ObservableCollections with the option to dynmically having them updated using an Edit window and binding, without having the global collections updated while making changes on the bound controls, so far the best solution seems to be : Clone the entity, detach the old one, attach the new one, mark it updated and save.

But oops, the following exception follows:

The relationship manager supplied by the object implementing IEntityWithRelationships is not the expected relationship manager.

Does someone knows how to solve this ?

b.t.w - Please dont offer to use serialization, nor reflection or the Matthieu MEZIL entity cloner as from performance issue (takes several seconds to copy the whole entitygraph).

开发者_运维问答

Thanks, Oran


I would question your approach. "takes several seconds to copy the whole entity graph" should be ringing some alarm bells. It sounds like you are storing the most of your data in memory. Just because you can doesn't mean you should. I would try re-architect you use deferred loading.

However that being said, are you using decoupled entities (The POCO approach)? This might help in this regard since they decouple from the context more and this might give you the flexibility to bring entities in and out of the context.


The above answer(Slappy) has lead me to the solution. Here is a full explanation :

  1. In order to Edit an entity and not effect the observables controls which are bound to it, i just wanted to clone the entity, perform changes, attach it to the object context and save. But i bumped my head against the wall as i did not succeed in this, so called, simple operation when using the default .NET MemberWiseClone() or any other cloning procedure i found around.

  2. When i serialized the object, i did succeed, but with one major problem : It took several seconds to serialize an object, a thing that was impossible out of performance issue.

  3. Then Slappy answer lead me to realize that i did not learned enough the Object Context behavior and how it handles the entity model. When i looked closely into the documents and followed the entity behavior, i realized i was contradicting myself - I tried to perform a Deep Clone on a 'live' and 'attached' context entity, a thing which took several seconds as pair the size of my data.

  4. The following procedure has solved my issue : I downloaded the (GREAT!) entity cloner from Matthieu MEZIL ( http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx ) , and used the CloneEntityWithGraph to fully clone the entity, but this time with one difference : I have detached the entity before trying to clone it. Ofcourse the entity was instantly cloned with all its graph in a way it could be later applied to the original entity as follows. After performing the changes, i have used the following code to successfully save changes :

    objectContext.ApplyCurrentValues(<ObjectSet Name>,<Cloned And Edited Entity>); objectContext.SaveChanges();

That's it! The entity was saved to the database successfully.

I prefer that approach from the POCO approach or from hard coding an edit into the speific entity properties as it is general and (should) be generally applicable to all my entities with out any specific coding for an entity.

Thanks for all the help, Oran

0

精彩评论

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