How to suppress ambient transaction warning with EF7 RC2?
The SuppressAmbientTransactionWarning() method cannot be found.
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.
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);
SqlServerOptionsExtension
does not contain a definition for ThrowOnAmbientTransaction
.