DotNetCore cross-platform ~ EFCore discards Context. Database. BeginTransaction replaced by TransactionScope,
TransactionScope is. net platform-based Distributed Transaction component, which is a local transaction by default, and can be automatically upgraded to a distributed transaction when necessary by the system. The prerequisite for the system is to enable the MSDTC Service, if necessary, you need to add the hosts ing between the database server and the application server. These have been written in many articles and will not be mentioned here.
Previous understanding and summary of TransactionScope
26th the uncertain will be changed to OK ~ When Will transactionscope be upgraded to a distributed transaction?
27th the uncertain will be changed to OK ~ When is transactionscope upgraded to a distributed transaction ~ Continued
28th the uncertain will be changed to OK ~ When is transactionscope upgraded to a distributed transaction ~ Continue (avoid unnecessary MSDTC)
37th the uncertain will be changed to OK ~ When is transactionscope upgraded to a distributed transaction ~ Different from SQL2008, SQL2005
38th the uncertain will be changed to OK ~ When Will transactionscope be upgraded to a distributed transaction? (How to upgrade the sql2005 database to MSDTC)
On the efcore platform, you may encounter an exception when using TransactionScope. Microsoft will prompt you to view the relevant information. This time the information is accurate! Https://docs.microsoft.com/en-us/ef/core/saving/transactions
This article mainly introduces the following content:
Savechanges is still a transaction
Like ef, The saveChanges () operation is itself a transaction block, and Uncle warehouse is used to store each operation in its own saveChanges, to hide the savechanges of the data context, if you want to insert two storages, you need to add an outer transaction to ensure data consistency, then Microsoft provides a solution.
2. Implement transactions in a Single Context
For a data context, if you have multiple savechanges, you can useContext. Database. BeginTransaction () to implement transactions.
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 } } }
Implement transactions between three different contexts
For the previous TransactionScope, if it is a different data context, we cannot implement transaction operations. Some can say that it should be upgraded to distributed, but for EF, it is implemented differently, but after the efcore era, this problem has been solved!
Cross-context transaction (relational databases only)You can also share a transaction across multiple context instances. This functionality is only available when
using a relational database provider because it requires the use of DbTransaction and DbConnection,
which are specific to relational databases.
As described above, a transaction across data contexts can be implemented, only supported by relational databases! This function is very necessary, but the instance shown below is for a data context, not for multiple contexts.
A cross transaction is not a transaction between two databases.
using (var context1 = new BloggingContext(options)) { using (var transaction = context1.Database.BeginTransaction()) { try { context1.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" }); context1.SaveChanges(); using (var context2 = new BloggingContext(options)) { context2.Database.UseTransaction(transaction.GetDbTransaction()); var blogs = context2.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 } } }
If multiple contexts are used for transactions, the following problems may occur:
var options = new DbContextOptionsBuilder<DemoContext>() .UseMySql("Server=localhost;DataBase=test2;UID=root;Password=root;charset=utf8;port=3306;SslMode=None") .Options; using (var context = new DemoContext(options)) { using (var transaction = context.Database.BeginTransaction()) { var user = new UserInfo { AddTime = DateTime.Now, Email = "test@sina.com", UserName = "test" }; context.UserInfo.Add(user); context.SaveChanges(); using (var context2 = new TaxContext()) { context2.Database.UseTransaction(transaction.GetDbTransaction()); context2.UserInfo.Add(new UserInfo { AddTime = DateTime.Now, Email = "tax_test", UserName = "tax" }); context2.SaveChanges(); } transaction.Commit(); } }
The following exception occurs: tell you that your database connection is not the current connection.
System.InvalidOperationException:“The specified transaction is not associated with the current connection.
Only transactions associated with the current connection may be used.”
I don't know when EF can solve the problem of multi-database transactions. At present, you can use eventually consistent distributed transactions to do this, however, we still look forward to Microsoft providing us with a simpler solution and a transaction.
Whether the database is distributed depends on whether the server where the database is located is the same, rather than whether the database connection strings are consistent!
Thank you for your complete explanation!