Search code examples
c#sql-serverlinqisolation-level

How to get isolation level in EF for MSSQL?


I am using EF for access MS SQL data with help LINQ. How can I get current isolation level ? For example this code:

var level = 
Database.Connection.UnderlyingTransaction.IsolationLevel.ToString();

give me error - System.NullReferenceException: Object reference not set to an instance of an object.

As I understand - UnderlyingTransaction is null because I don't use any transactions in my LINQ code.

UPDATE: I need current isolation level of current LINQ request without creating any transaction by my hand ...


Solution

  • In EF Core this works:

    System.Transactions.Transaction.Current.IsolationLevel
    

    It's showing Serializable for me by default if I don't specify a level, and does show the updated value if I set it explicitly.

    using (var tran = new TransactionScope(TransactionScopeOption.RequiresNew,
           new TransactionOptions
           {
    
             //IsolationLevel = IsolationLevel.ReadCommitted,
             Timeout = TimeSpan.FromMinutes(1),
    
           }, TransactionScopeAsyncFlowOption.Enabled)) { }