How to suppress ambient transaction warning with EF7 RC2

.net-core-rc2 entity-framework-core

Question

How to suppress ambient transaction warning with EF7 RC2?

The SuppressAmbientTransactionWarning() method cannot be found.

1
2
6/14/2016 6:58:46 PM

Accepted Answer

You can disable that in the DbContext(in old versions):

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
     optionsBuilder.UseSqlServer(@"Server=.\;Database=EFTutorial;Trusted_Connection=True;").SuppressAmbientTransactionWarning();
     base.OnConfiguring(optionsBuilder);
}

or

If you have the connection string in the json:

var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]).SuppressAmbientTransactionWarning();

In the new version:

  var optionsBuilder = new Microsoft.EntityFrameworkCore.DbContextOptionsBuilder();
    var extension = new SqlServerOptionsExtension(optionsBuilder.Options.GetExtension<SqlServerOptionsExtension>())
    {
        ThrowOnAmbientTransaction = false
    };

    ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
0
6/15/2016 10:25:59 PM

Popular Answer

While the question was related to RC2, as of Oct 16, 2016, this worked for me and hopefully it is useful to someone else:

services.AddDbContext<OpenDataContext>(options =>
   {
       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
       options.ConfigureWarnings(x => x.Ignore(RelationalEventId.AmbientTransactionWarning));
   });

Used above in an integration test setup class.



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