I'm trying to rewrite old library to use EntityFramework Core and I can't figure out how to begin transaction with specific isolation level.
Previously I was able to do something like this:
DbContext.Database.BeginTransaction(IsolationLevel.Snapshot);
What is alternative implementation in the EntityFramework Core?
One of the biggest reasons not to use Entity Framework Core is that your application needs the fastest possible data access. Some applications do a lot of heavy data operations with very high-performance demands, but usually business applications don't have that high of a performance demand.
Share connection and transaction You can now create multiple context instances that share the same connection. Then use the DbContext. Database. UseTransaction(DbTransaction) API to enlist both contexts in the same transaction.
In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.
The EF Core code is exactly the same.
DbContext.Database.BeginTransaction(IsolationLevel.Snapshot);
The only difference is that in EF Core the method with isolation level (as many others) is an extension method, defined in RelationalDatabaseFacadeExtensions class, and importantly, located in Microsoft.EntityFrameworkCore.Relational assembly.
So if you have using Microsoft.EntityFrameworkCore;
and don't see it, add reference to the Microsoft.EntityFrameworkCore.Relational.dll
assembly / package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With