Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework + Autofac - Random errors on save
    text
    copied!<p>Using autofac as my IoC framework.<br> I'd like to be able to set up my <code>DbContext</code> instance in my application's startup. </p> <p>In my ASP.NET MVC 3 project, I register <code>DbContext</code> instance in Global.asax (<code>PerLifetimeScope</code>). But when I fire up my site on multiple browsers (or multiple tabs) at once, <strong>sometimes</strong> I get <code>Object reference not set to an instance of an object.</code> or <code>New transaction is not allowed because there are other threads running in the session</code> when I try to save changes back to database. Also I get <code>ExecuteReader requires an open and available Connection. The connection's current state: Broken.</code> sometimes when I want to read data from database.</p> <p>the errors seem to pop up randomly and I suspect it has something to do with my context's lifetime scope. here's my DbContext's overriden <code>SaveChange</code> method. </p> <pre><code>public class MyContext : DbContext { public override int SaveChanges() { var result = base.SaveChanges(); // Exception here } } </code></pre> <p>Here's how I register my context: </p> <pre><code>builder.Register(c =&gt; new MyContext("SomeConnectionString")) .InstancePerLifetimeScope(); </code></pre> <p>If I just have one open tab of my site in the browser everything works ok.<br> Also, It's worth mentioning I have CRUD operations with db every 5-10 seconds in my website by calling a controller method using Ajax. </p> <p>StackTrace for <code>New transaction is not allowed because there are other threads running in the session</code>:</p> <pre><code>at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel) at System.Data.EntityClient.EntityConnection.BeginTransaction() at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at MyProject.Data.MyContext.SaveChanges() in D:\Test.cs </code></pre> <p>StackTrace for <code>Object reference not set to an instance of an object.</code>: </p> <pre><code>at System.Data.Objects.ObjectStateManager.DetectConflicts(IList`1 entries) at System.Data.Objects.ObjectStateManager.DetectChanges() at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force) at System.Data.Entity.Internal.InternalContext.GetStateEntries(Func`2 predicate) at System.Data.Entity.Internal.InternalContext.GetStateEntries() at System.Data.Entity.Infrastructure.DbChangeTracker.Entries() at System.Data.Entity.DbContext.GetValidationErrors() at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at MyProject.Data.MyContext.SaveChanges() in D:\Test.cs at </code></pre>
 

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