In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.