Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeginTransaction with IsolationLevel in EF Core

Tags:

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?

like image 898
Pavel Avatar asked Mar 17 '19 14:03

Pavel


People also ask

When should you not use Efcore?

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.

How do I create a transaction in EF core?

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.

What's the point of multiple SaveChanges inside an Entity Framework core 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.


1 Answers

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.

like image 171
Ivan Stoev Avatar answered Sep 25 '22 00:09

Ivan Stoev