title: ASP.NET Web Forms and MySql Entity Framework: "Nested transactions are not supported"
body: <p>I have an ASP.NET web forms application, using the .Net Connector for MySql Entity Framework.</p>
  
  <p>I've made a web application using MySql in the past, and never ran into this issue.</p>
  
  <p>I'm currently getting ~20000 page views a day, and each day about 10 errors of 'Nested transactions are not supported.'</p>
  
  <p>I can't figure out why it happens at all, and why it happens only once in awhile.</p>
  
  <p>In a Master page Preload event I get an integer ID for the user and save it to a public variable:</p>
  
  <pre><code>using (var entity = new Entities())
                  {
                      var user = entity.my_aspnet_users.Single(i =&gt; i.name == Context.User.Identity.Name);
                      UserID = user.id;
                      if (HttpContext.Current.Request.Url.AbsolutePath != "###" &amp;&amp; HttpContext.Current.Request.Url.AbsolutePath != "###")
                      {
                          if (user.Player == null)
                              Response.Redirect("###", true);
                          else
                              PlayerID = user.Player.PlayerID;
                      }
                  }
  </code></pre>
  
  <p>And then on the page:</p>
  
  <pre><code>using (var entity = new Entities())
                              if (entity.Players.Count(i =&gt; ###) == 1)
                              {
                                  var user = entity.my_aspnet_users.Single(i =&gt; i.id == Master.UserID);
                                  if (user.Player == null)
                                  {
                                      user.Player = entity.Players.Single(i =&gt; ###);
                                      entity.SaveChanges();
                                  }
                                  Response.Redirect("###", false);
                              }
  </code></pre>
  
  <p>The error gets thrown on SaveChanges().  I don't know why it only happens sometimes.  I also sometimes get the same error when a user registers on the site, though all this seems random and rare.</p>
  
  <p>Anyway, for one, I thought nested transactions were allowed in MySql?  Is there a setting I need to set?  My MySql instance is 5.6.17 and I believe using default parameters - set up in Amazon RDS.</p>
  
  <p>Does 'User.Identity.Name' access the database?  Do you have any idea on where I can look to solve this?</p>
  
  <p>Here's the stack trace of some exceptions if helpful:</p>
  
  <pre><code>**Exception on example code:**
  System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---&gt; System.Data.Entity.Core.EntityException: An error occurred while starting a transaction on the provider connection. See the inner exception for details. ---&gt; System.InvalidOperationException: Nested transactions are not supported.
  
  at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
  
  at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
  
  at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction(IsolationLevel iso)
  
  at MySql.Data.MySqlClient.MySqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
  
  at System.Data.Common.DbConnection.BeginTransaction(IsolationLevel isolationLevel)
  
  at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__0(DbConnection t, BeginTransactionInterceptionContext c)
  
  at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
  
  at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.BeginTransaction(DbConnection connection, BeginTransactionInterceptionContext interceptionContext)
  
  at System.Data.Entity.Core.EntityClient.EntityConnection.&lt;&gt;c__DisplayClassf.b__d()
  
  at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
  
  at System.Data.Entity.Core.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
  
  --- End of inner exception stack trace ---
  
  at System.Data.Entity.Core.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
  
  at System.Data.Entity.Core.EntityClient.EntityConnection.BeginTransaction()
  
  at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
  
  at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
  
  at System.Data.Entity.Core.Objects.ObjectContext.&lt;&gt;c__DisplayClass2a.b__27()
  
  at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
  
  at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
  
  at System.Data.Entity.Core.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 ###.Account.###.ButtonSubmit###_Click(Object sender, EventArgs e)
  
  at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
  
  at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
  
  at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  
  at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  
  at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  
  at System.Web.UI.Page.HandleError(Exception e)
  
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  
  at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  
  at System.Web.UI.Page.ProcessRequest()
  
  at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
  
  at System.Web.UI.Page.ProcessRequest(HttpContext context)
  
  at ASP.account_###.ProcessRequest(HttpContext context)
  
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  
  
  
  **Exception on creating a user:**
  System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---&gt; System.InvalidOperationException: Nested transactions are not supported.
  
  at MySql.Data.MySqlClient.ExceptionInterceptor.Throw(Exception exception)
  
  at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex)
  
  at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction(IsolationLevel iso)
  
  at MySql.Data.MySqlClient.MySqlConnection.BeginTransaction()
  
  at MySql.Web.Security.MySQLMembershipProvider.GetUser(Object providerUserKey, Boolean userIsOnline)
  
  at MySql.Web.Security.MySQLMembershipProvider.GetUser(String username, Boolean userIsOnline)
  
  at MySql.Web.Security.MySQLMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus&amp; status)
  
  at System.Web.UI.WebControls.CreateUserWizard.AttemptCreateUser()
  
  at System.Web.UI.WebControls.CreateUserWizard.OnNextButtonClick(WizardNavigationEventArgs e)
  
  at System.Web.UI.WebControls.Wizard.OnBubbleEvent(Object source, EventArgs e)
  
  at System.Web.UI.WebControls.CreateUserWizard.OnBubbleEvent(Object source, EventArgs e)
  
  at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
  
  at System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
  
  at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
  
  at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
  
  at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
  
  at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
  
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  
  at System.Web.UI.Page.HandleError(Exception e)
  
  at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  
  at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  
  at System.Web.UI.Page.ProcessRequest()
  
  at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
  
  at System.Web.UI.Page.ProcessRequest(HttpContext context)
  
  at ASP.register_aspx.ProcessRequest(HttpContext context)
  
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>