I'm trying to create dependent transactions in Entity Framework Core (v. 2.1).
The idea is to create "children" transactions inside a "father" transaction.
If one of the childs fails, so will have the father, too.
I've tried with TransactionScope
, but I've got the following error.
Warning as error exception for warning 'Microsoft.EntityFrameworkCore.Database.Transaction.AmbientTransactionWarning': An ambient transaction has been detected. Entity Framework Core does not support ambient transactions. See http://go.microsoft.com/fwlink/?LinkId=800142 To suppress this Exception use the DbContextOptionsBuilder.ConfigureWarnings API. ConfigureWarnings can be used when overriding the DbContext.OnConfiguring method or using AddDbContext on the application service provider.
The followings are controller's methods, the dbcontext is created and initialized in the constructor of the controller
Here's my code:
public bool father()
{
using (var transaction = new TransactionScope(TransactionScopeOption.Required))
{
try
{
//Do Stuff with the dbcontext
//(read, as "var x1 = _dbcontext.Database.Table1; etc...")...
child();
transaction.Complete();
//Do Stuff...
}
catch
{
//Do Stuff...
transaction.Dispose();
//Do Stuff...
}
}
}
}
public bool child()
{
using (var transaction2 = new TransactionScope(TransactionScopeOption.Required))
{
try
{
//Do other stuff with the dbcontext
//(read, as "var x2 = _dbcontext.Database.Table2; etc...")...
transaction2.Complete();
//Do Stuff...
}
catch
{
//Do Stuff...
transaction2.Dispose();
//Do Stuff...
}
}
}
}
How can I make it work?