How to avoid a "Nested transactions are not supported." error?

.net c# entity-framework mysql

Question

I am using EF6 to do some pretty simple integration with a MySql database.

The Nested transactions are not supported. error occurs after I do the following:

  1. Attempt to add a key that already exists... Which leads to the error: Duplicate entry 'asdf' for key 'UserName_UNIQUE'
  2. Attempt to add anything afterwards... Which leads to the error: Nested transactions are not supported.

I guess I'm not sure what would be Nested about these two queries... What am I doing wrong:

And for some code

using (var db = C2SCore.BuildDatabaseContext())
{
  db.Users.Add(new UserProfile { UserName = UserName, Password = Password });
  db.SaveChanges(); // <- Errors occur here...
}

This snippet runs (as my the flow described above implies) for every UserProfile I add.

1
9
1/29/2014 9:27:50 PM

Popular Answer

I have exactly the same problem. Try the following workaround by wrapping it into a TransactionScope:

using System.Transactions; // Add assembly in references

using (var db = C2SCore.BuildDatabaseContext())
{
  using (var tran = new TransactionScope())
  {
    db.Users.Add(new UserProfile { UserName = UserName, Password = Password });
    db.SaveChanges(); // <- Should work now after first exception
    tran.Complete();
  }
}

<package id="MySql.Data" version="6.8.3" targetFramework="net45" />
<package id="MySql.Data.Entities" version="6.8.3.0" targetFramework="net45" />

However, they are aware of it: http://bugs.mysql.com/bug.php?id=71502

6
7/16/2014 2:33:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow