I'm trying to get EF 6's default transaction isolation level to use snapshot isolation, but it's not working and always using Readcommitted. Here's what I've done so far:
I've enabled SQL Server snapshot isolation on my SQL Server 2014 database by executing the following commands:
ALTER DATABASE MyDb
SET READ_COMMITTED_SNAPSHOT ON
GO
ALTER DATABASE MyDb
SET ALLOW_SNAPSHOT_ISOLATION ON
GO
I have a DbContext implementation and have written the following Linqpad script to output the isolation level that's being used, and it's always showing "Readcommitted" which is bad. Here's the Linqpad script:
void Main()
{
this.Database.SqlQuery<string>(@"SELECT CASE transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'ReadUncomitted'
WHEN 2 THEN 'Readcomitted'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot' END AS TRANSACTION_ISOLATION_LEVEL
FROM sys.dm_exec_sessions
where session_id = @@SPID
").Single().Dump();
}
I've seen people explicitly setting the transaction isolation level by newing up transactions, but I'd like to set snapshot isolation as the default transaction type, like maybe in the OnModelCreating override in my DbContext. Is that possible? Or do you always have to do this in explicit transactions? We're using Unity IoC to inject our DbContext for us, and we're not declaring any explicit transactions anywhere in our code currently...