How to avoid a "Nested transactions are not supported." error?
Solution 1
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
Solution 2
Please I just want to reiterate "they are aware of it" but without people signing up and bumping the thread it is likely going to take longer to resolve when this is an urgent issue.
http://bugs.mysql.com/bug.php?id=71502
Solution 3
Try adding a db.Connection.Open()
at the start and db.Connection.Close()
at the end.
Solution 4
Seeing how it is now 2017 and this issue still exists I am going to post the only workaround I have been able to find that does not involve pulling the source code. Adding ConnectionReset=True to the connection string will resolve the issue. It should be noted this will incur an additional round trip to the database for every connection used in the connection pool.
Here is an example: server=localhost;port=3306;database=somedatabase;uid=someuser;password=itsasecret;ConnectionReset=True;
Related videos on Youtube
poy
Updated on June 04, 2022Comments
-
poy almost 2 years
I am using
EF6
to do some pretty simple integration with aMySql
database.The
Nested transactions are not supported.
error occurs after I do the following:- Attempt to add a
key
that already exists... Which leads to the error:Duplicate entry 'asdf' for key 'UserName_UNIQUE'
- 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.-
rae1 over 10 yearsWhere is the duplicate
key
code? -
poy over 10 years@rae1 The
UserName
is thekey
... If that's what you're asking. -
user2864740 over 10 yearsWhat happens if explicitly opening up a wrapping TransactionScope? And there isn't a context open from elsewhere, is there?
-
rae1 over 10 yearsWhere does the duplicate
key
error occurs and where does the "Nested transactions ..." error occurs? -
poy over 10 years@rae1 They both occur at the
db.SaveChanges()
-
poy over 10 years@user2864740 I don't have the
TransactionScope
class available to me... Do you have to do something special to get it? -
user2864740 over 10 years@Andrew See TransactionScope (it is from System.Transactions); with an ambient TS present I believe all the EF operations will use it (and if it is aborted it can't be subsequently used). However, this sort of error normally comes when using a deferred LINQ/fetch operation - which is why I wonder if there is another context (that is indirectly accessible) lurking about.
-
static-max almost 10 yearsI have the exactly the same problem. However, they are aware of it: bugs.mysql.com/bug.php?id=71502
-
Luke over 9 yearsI've got this problem too. Any solution!? Seems like this Mysql/C# stuff is a duff!
- Attempt to add a
-
user2864740 over 10 yearsThere is a
using
around the context object and the code provided does not indicate any context leakage. -
AronVanAmmers about 9 yearsThis is the only workaround for this I found, thanks. It does require a lot of changes to your code however as every operation with a
DbContext.SaveChanges()
has to be wrapped in aTransactionScope
. Could be useful if your app just has a few of those though. -
AronVanAmmers about 9 yearsCorrection: that doesn't work for me. The exception doesn't happen, but the data isn't saved to the database.