Matt
Matt

Reputation: 26971

Must EF4 Entities be saved by the same thread that created them?

Must EF4 Entities be saved by the same thread that created them? I seem to lose my changes or my code throws an ObjectContext exception of I do so.

The reason I'm asking is that this seems to be the only condition I can find that would cause an issue.

If this is the case, can they at least be modified by other threads and then saved by the creating thread?

Upvotes: 1

Views: 79

Answers (1)

Eranga
Eranga

Reputation: 32437

ObjectContext class is not thread safe (See the remarks section in msdn). Modifying entities attached to the context in different threads is also not advisable since the ObjectStateManager is also not thread safe. The main thread may have disposed the context when other threads are accessing the entities thereby throwing disposed exceptions.

You can try detaching the entities and then modifying the entity in other threads and re attaching them. Recommended practice is to use context per thread.

Upvotes: 2

Related Questions