We have a pretty heavy module on one of our internal applications which is using SignalR to check for new notifications. We have been advised that we are creating locks on the database and that we should take an uncommitted read for the method in question.
This is not something I've had to do before, and as far as i'm aware EF by default takes a committed read of records. So i've wrapped the existing query in another using statement like so:
public IEnumerable<ClientUpdates> GetNotifications(int forPaId)
{
using (var transactionScope =
new TransactionScope(TransactionScopeOption.Required, GetReadUncommitedTransactionOptions()))
{
using (var ctx = ContextFactory.CompanyDb)
{
// Do stuff
transactionScope.Complete();
return objectList;
}
}
}
private TransactionOptions GetReadUncommitedTransactionOptions()
{
var transactionOptions = new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadUncommitted
};
return transactionOptions;
}
This results in the following error message:
InvalidOperationException: 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.
So from that I tried to suppress the warning:
services.AddDbContext<DbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString"));
options.ConfigureWarnings(x => x.Ignore(RelationalEventId.AmbientTransactionWarning));
});
Which results in this error message:
NotSupportedException: Enlisting in Ambient transactions is not supported.
So now I'm a little lost, I can see others have logged this issue on the official issue tracker, but I don't understand what the actual issue is, and how I can force this particular module to do a "Dirty" read of the data, so as to stop the locks happening.