如何理解EF Core事务提交
发表于:2024-09-23 作者:千家信息网编辑
千家信息网最后更新 2024年09月23日,本篇内容主要讲解"如何理解EF Core事务提交",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何理解EF Core事务提交"吧!控制事务可以使用 DbC
千家信息网最后更新 2024年09月23日如何理解EF Core事务提交
本篇内容主要讲解"如何理解EF Core事务提交",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何理解EF Core事务提交"吧!
控制事务
可以使用 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 时,可能会引发其他提供程序类型或不执行任何操作。
使用 System.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 } }
还可以在显式事务中登记。
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 的限制
EF Core 依赖数据库提供程序以实现对 System.Transactions 的支持。 如果提供程序未实现对 System.Transactions 的支持,则可能会完全忽略对这些 API 的调用。 SqlClient 支持它。
自 .NET Core 2.1 起,System.Transactions 实现不包括对分布式事务的支持,因此不能使用 TransactionScope 或 CommittableTransaction 来跨多个资源管理器协调事务。
到此,相信大家对"如何理解EF Core事务提交"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
事务
支持
程序
内容
数据
数据库
学习
查询
实用
更深
较大
两个
作用
兴趣
分布式
单个
多个
实用性
实际
操作简单
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
公安加强网络安全管理的措施
机房服务器不用稳压器可以吗
pd数据库表怎么建立联系
残联网络安全排查报告
办公室网络安全策略
软件开发详解视频教程
淄博软件开发编程
电子合同软件开发公司
linux数据库登录
江西局域网存储服务器
什么网络技术最适合多媒体通信
问津网络技术有哪些游戏
yii1.0 数据库
互联网科技行业ppt
网络安全在哪里查询
软件开发厂商交付给用户
口碑最好的app软件开发
iso软件开发工具
建立数据库的好处酒店
临沂联想服务器总代理供应商
软件开发接口文档如何查看
强大的数据库服务器
软件开发工作室需要办理什么证
儿童视频软件开发
服务器主板风扇接口
浏览器怎么加速国外服务器
暑假网络安全篇
r4烧录卡内核数据库
cssci是哪个数据库的
宝山区威力网络技术售后服务