c# - SQL Server 和实体框架:事务在一个用例中失败
问题描述
我在我的代码中使用 C#、SQL Server、Entity Framework 6。在表中插入记录时出现异常,但记录已插入表中。
在执行 operation1 时出现以下错误:
System.Data.SqlClient.SqlException:超时已过期。在操作完成之前超时时间已过或服务器没有响应。
该语句已终止。System.ComponentModel.Win32Exception:等待操作超时
这个问题很少发生。
我的代码如下所示:
Public Void UploadData(Request request)
{
// Creating db context.
// Creating TransactionScope object.
TransactionOptions _oTransactionOptions = new TransactionOptions();
oTransactionOptions.IsolationLevel = IsolationLevel.ReadCommitted;
oTransactionOptions.Timeout = new TimeSpan(hour, min, sec);
TransactionScope TransactionScope = new TransactionScope(TransactionScopeOption.Required, oTransactionOptions);
try
{
// Doing operation1
// Doing operation2
// Doing operation3
// Doing operation4
TransactionScope.Complete();
TransactionScope.Dispose();
}
catch (Exception ex)
{
TransactionScope.Dispose();
// logging error.
}
finally
{
// Disposing context.
}
}
解决方案
推荐阅读
- javascript - 如何将变量放在引号JS中
- firebase - 从 OneSignal 迁移到 Firebase 推送通知
- sql-server - 在 SQL Server 中导入 .sqlserver 文件
- windows - 无法在 Windows 10 上更改 psql 中的目录。PostgreSQL 11
- c - 找出负二进制数和正二进制数之间的差异
- java - JPA 枚举继承
- reactjs - 打开子页面(弹出页面)时如何禁用滚动父组件?- CSS
- sql-server - SQL Server 问题中的 MIN 函数
- python - 遍历 pandas 数据框,使用 if 语句检查每个列值,并将列值传递给空 df 的首选列
- elasticsearch - 编辑 configmap.yml 和 elasticsearch.yml 以更改密码