1

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?

13
  • What is the EFCore version you are using? Commented May 29, 2018 at 13:31
  • @user1672994 EFCore 2.1 Commented May 29, 2018 at 13:32
  • 2
    @SantaCloud .NET Core 2.1 goes RTM on Thursday, with the binaries already available for local installation. Perhaps you only need to wait a couple of days? Commented May 29, 2018 at 14:02
  • 2
    If you really want to, you can try the Early Access libraries which should (but doesn't have to) be same as those we will get on Thursday. Details here: github.com/aspnet/Home/wiki/2.1.0-Early-Access-Downloads Commented May 29, 2018 at 16:03
  • 1
    @SantaCloud +1 for the username :) Commented Sep 20, 2018 at 6:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.