Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Without seeing your code, the short answer is that EntityFramework is not thread-safe. You're getting this error in a seemingly random pattern when two+ threads overlap when trying to try access your <code>ObjectContext</code>. I assume you've stuffed your context into a <code>static</code> variable. Either make the context a local variable or write locking around access to the <code>ObjectContext</code>.</p> <p>If you want a more specific answer posting your code will help.</p> <p><strong>Edit</strong></p> <p>Your issue is that two threads are trying to use the context at the same time, or 1 thread is leaving a transaction open then and a second thread comes through trying to use your singleton context. Your code snippet raises more questions for me than I had before. </p> <ul> <li>In your code example is _<code>lockObject</code> a static variable? </li> <li>Why do you show the lock in <code>SaveChanges</code> but then explain an error is being thrown from <code>InsertOrUpdateDetachedObject</code>? Can we see the code for <code>InsertOrUpdateDetachedObject</code>?</li> <li>Does SaveChanges use the same <code>_lockObject</code> as all other methods that access the context directly?</li> <li>Is your call to <code>_context.SaveChanges</code> the only way you save to the DB or do you have other areas that open transaction contexts on their own? </li> <li>Your using a singleton so your context is shared across multiple calls. It might be preferable to instantiate a new new context for every WFC call.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload