Does EF Core support TransactionScope?

Viewed 1169

I have .NET Core 3.1 and EntityFrameworkCore 3.1.8.

In a command (which writes data to db) I use SaveChanges() method multiple times. Therefore, I decided to make all these transactional and use TransactionScope() like this:

using (var scope = new TransactionScope())
{
}

Is this a correct approach? Will this work correctly?

Note: I have one DbContext

1 Answers

Yes, TransactionScope would most likely work for you. As a personal recommendation, I would use DbContext.Database.BeginTransaction() instead.

The reason for this is because TransactionScope must be implemented by the library you are using. In general, SDKs for relational databases should support TransactionScope (I know Microsoft.Data.SqlClient for SQL Server does, not sure about the other databases).

Source: https://docs.microsoft.com/en-us/ef/core/saving/transactions#using-systemtransactions

Related