Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework without Transaction?

Is there a way to use EF without transaction? I have very simple single insert and don't want to roll-back when something goes wrong as there may be a trigger logging then raising error from the DB side which I have no control over. I just want to insert then catch any exceptions but don't want to roll-back.

like image 853
Sue Avatar asked Dec 17 '09 12:12

Sue


People also ask

Why you shouldn't use Entity Framework with transactions?

No process will be able to access the tables you have touched (even reading from it) during your transaction. That can lead to Deadlocks pretty fast and you want to avoid them at all costs! Rule of Thumb: Save only once per task and don't use transactions.

Does EF core use transaction?

This feature was introduced in EF Core 5.0. When SaveChanges is invoked and a transaction is already in progress on the context, EF automatically creates a savepoint before saving any data. Savepoints are points within a database transaction which may later be rolled back to, if an error occurs or for any other reason.

When should you not use Entity Framework?

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.

Is SaveChanges a 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.


2 Answers

We are not aware of any way to get rid of transactions in Entity Framework CUD operations.

like image 102
Devart Avatar answered Sep 20 '22 07:09

Devart


using( var transation = new TransactionScope(TransactionScopeOption.Suppress) )
{
    ObjectContext.SaveChanges();
}
like image 38
John Farrell Avatar answered Sep 21 '22 07:09

John Farrell