Cloning EF ObjectContext changes to another new context to save async changes safely

I first use EF 4.0 without code, I need to do the async save operation, the problem is that the ObjectContext is not thread safe, and my context has many objects related to the WPF interface, I think about cloning the change of my objects to another context and it’s safe to save this context in another thread,

Is there an easy way to clone changes? I know the keyword is ObjectStateManager, but how can I do this?

0
multithreading c # visual-studio-2010 entity-framework
source share
1 answer

I am sure this is not possible. ObjectStateManager is the heart and soul of tracking and saving changes for an ObjectContext and is closely intertwined with context. This will require a simple transplant operation. From a technical point of view, you will not be able to clone one without using tons of reflection, because most of its state is managed domestically. And then ObjectContext.ObjectStateManager does not have a setter.

The root of your problem is to have a context to represent (at least maybe even one global context?). Context per view is a viable option for rich client applications, but requires asynchronous processing of parameter changes. Longer context life is no longer an option.

The solution may be to bind (disabled) view models to the user interface, instead of entity objects, and create an instance of the context in a separate thread (well, Task ) to save the changes.

You might be interested in another new feature in EF 6 (now in alpha): the built-in async . I have not experimented with this yet, but it looks promising.

+1
source share

All Articles