作者:conan 2021-03-17 00:05:50
开发
前端
分布式 虽然所有关系数据库提供程序都支持事务,但在调用事务 API 时,可能会引发其他提供程序类型或不执行任何操作。
本文转载自微信公众号「后端Q」,作者conan。转载本文请联系后端Q公众号。
可以使用 DbContext.Database API 开始、提交和回滚事务。 以下示例显示了在单个事务中执行的两个 SaveChanges 操作以及一个 LINQ 查询:
- 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
- }
虽然所有关系数据库提供程序都支持事务,但在调用事务 API 时,可能会引发其他提供程序类型或不执行任何操作。
如果需要跨较大作用域进行协调,则可以使用环境事务。
- 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
- }
- }
还可以在显式事务中登记。
- 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
- }
- }
名称栏目:深入分析EFCore事务提交,分布式事务
本文网址:http://www.shufengxianlan.com/qtweb/news2/392102.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联