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?

Santa Cloud
  • 547
  • 1
  • 10
  • 22
  • What is the EFCore version you are using? – user1672994 May 29 '18 at 13:31
  • @user1672994 EFCore 2.1 – Santa Cloud May 29 '18 at 13:32
  • EF Core *2.1* [isn't released yet](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/), it's still in RC1. How did you add it? – Panagiotis Kanavos May 29 '18 at 13:47
  • @Deadpool suppressing a *runtime* warning that says transactions don't work is a very bad idea. Besides if the compiler *warns* you , you should listen and actually treat the warning as if it were an error – Panagiotis Kanavos May 29 '18 at 13:48
  • @PanagiotisKanavos I agree, but after I read the ef core 2.1 docs I found out that transactions are native in ef core 2.1 and googling the error I just had results about suppressing it. Personally, I would give a try... Alternatively, [the only way could be trying with declaring dbcontexts inside the transaction using](https://stackoverflow.com/questions/45919011/how-to-implement-ambient-transaction-in-entity-framework-core) – Deadpool May 29 '18 at 13:55
  • @PanagiotisKanavos I've installed the release candidate… Deadpool Thanks, but actually I agree with Panagiotis about suppressing the error. I've tried, but it haven't worked. Also, I prefer to not declare db contexts inside each method. I would do it as a last try but I would prefer other solutions. – Santa Cloud May 29 '18 at 13:59
  • @Deadpool EF Core 2.1 isn't released yet. It's the version that actually adds support for TransactionScope. Telling someone to supress warnings that say transactions aren't working doesn't help. – Panagiotis Kanavos May 29 '18 at 13:59
  • @PanagiotisKanavos You're right... I'm deleting the first comment in order to keep just the second one, which seems to be more useful – Deadpool May 29 '18 at 14:01
  • 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? – Panagiotis Kanavos May 29 '18 at 14:02
  • @PanagiotisKanavos Hope so… But if in the meanwhile I hope also that someone found a solution or correct me if there is actually something logically wrong in my code (about that, in your opinion, do you think it's correct as it is?) – Santa Cloud May 29 '18 at 14:07
  • 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: https://github.com/aspnet/Home/wiki/2.1.0-Early-Access-Downloads – Wiktor May 29 '18 at 16:03
  • 1
    @SantaCloud +1 for the username :) – danpop Sep 20 '18 at 06:55
  • @danpop Thanks man! :) – Santa Cloud Oct 09 '18 at 10:06

0 Answers0