Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand EF Core transaction commit

Shulou Source: shulou.com Published: 2022-06-03 05:08:04 09月13日 Update

This article focuses on "how to understand EF Core transaction commit". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand EF Core transaction commit.

Control transaction

You can use DbContext.Database API to start, commit, and roll back transactions. The following example shows two SaveChanges operations and a LINQ query performed in a single transaction:

Using var context = new BloggingContext (); using var transaction = context.Database.BeginTransaction (); try {context.Blogs.Add (new Blog {Url = "http://blogs.msdn.com/dotnet"}); context.SaveChanges (); context.Blogs.Add (new Blog {Url =" http://blogs.msdn.com/visualstudio"}); context.SaveChanges () Var blogs = context.Blogs .OrderBy (b = > b.Url) .Tolist (); / / Commit transaction if all commands succeed, transaction will auto-rollback / / when disposed if either commands fails transaction.Commit ();} catch (Exception) {/ / TODO: Handle failure}

Although all relational database providers support transactions, other provider types may be raised or no action may be performed when the transaction API is called.

Use System.Transactions

If you need to coordinate across a larger scope, you can use environmental transactions.

Using (var scope = new TransactionScope (TransactionScopeOption.Required, new TransactionOptions {IsolationLevel = IsolationLevel.ReadCommitted})) {using var connection = new SqlConnection (connectionString); connection.Open (); try {/ / Run raw ADO.NET command in the transaction var command = connection.CreateCommand (); command.CommandText = "DELETE FROM dbo.Blogs"; command.ExecuteNonQuery () / / Run an EF Core command in the transaction var options = new DbContextOptionsBuilder () .UseSqlServer (connection) .options; using (var context = new BloggingContext (options)) {context.Blogs.Add (new Blog {Url = "http://blogs.msdn.com/dotnet"}); context.SaveChanges () } / / Commit transaction if all commands succeed, transaction will auto-rollback / / when disposed if either commands fails scope.Complete ();} catch (Exception) {/ / TODO: Handle failure}

You can also enlist in an explicit transaction.

Using (var transaction = new CommittableTransaction (new TransactionOptions {IsolationLevel = IsolationLevel.ReadCommitted})) {var connection = new SqlConnection (connectionString); try {var options = new DbContextOptionsBuilder () .UseSqlServer (connection) .options; using (var context = new BloggingContext (options)) {context.Database.OpenConnection (); context.Database.EnlistTransaction (transaction) / / Run raw ADO.NET command in the transaction var command = connection.CreateCommand (); command.CommandText = "DELETE FROM dbo.Blogs"; command.ExecuteNonQuery (); / / Run an EF Core command in the transaction context.Blogs.Add (new Blog {Url = "http://blogs.msdn.com/dotnet"}); context.SaveChanges () Context.Database.CloseConnection ();} / / Commit transaction if all commands succeed, transaction will auto-rollback / / when disposed if either commands fails transaction.Commit ();} catch (Exception) {/ / TODO: Handle failure}} System.Transactions restrictions

EF Core relies on database providers for System.Transactions support. If the provider does not implement support for System.Transactions, calls to these API may be completely ignored. SqlClient supports it.

Since .NET Core 2.1, the System.Transactions implementation does not include support for distributed transactions, so you cannot use TransactionScope or CommittableTransaction to coordinate transactions across multiple resource managers.

At this point, I believe you have a deeper understanding of "how to understand EF Core transaction commit". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Transaction support program content data database learning query practical deeper larger two function interest distributed single multiple practical practical easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Microsoft Shulou Technology Shulou Information