英文:
What is a "User Initiated Transaction"?
问题
有许多网络解决方案可以使用 Entity Framework 并解决此错误:
“执行策略 'SqlServerRetryingExecutionStrategy' 不支持用户发起的事务”
我遇到了这个问题,并按照互联网上推荐的修复方法解决了我正在执行的那个 SQL 事务中的问题。问题是,我需要找到我的应用中可能引起相同问题的其他地方并修复它们。但当我不知道什么是“用户发起的事务”时,我该怎么做呢?我已经在互联网上搜索了很多地方,包括微软在此问题上的知识文章此处,但它们没有定义这个术语。
我“认为”这是当您使用 transaction scope
或 using BeginTransaction()
时发生的,但我宁愿不要猜测。
请有人定义一下这个术语吗?
英文:
There are many solutions on the web for using Entity Framework and getting this error:
> Execution strategy 'SqlServerRetryingExecutionStrategy' does not
> support user-initiated transactions
I ran into this issue and followed the recommended fixes on the internet and I no longer have the problem in that one sql transaction I was doing. The problem is, I need to find other areas of my app that could cause the same issue and fix them. How can I do this when I don't know what a "User initiated transaction" is. I've looked all over the web including Microsoft's knowlege article on this issue HERE but they don't define this term.
I "think" it is when you are using a transaction scope
, or a using BeginTransaction()
but I would rather not guess.
Can someone define this term please?
答案1
得分: 1
用户启动的事务是一个使用SqlTransaction
启动的数据库事务,您或一些代码启动的事务。例如:
using (SqlConnection objConn = new SqlConnection(strConnString))
{
objConn.Open();
objTrans = objConn.BeginTransaction();
SqlCommand objCmd1 = new SqlCommand("insert into tblProject values(1, 'TestProject')", objConn);
SqlCommand objCmd2 = new SqlCommand("insert into tblProjectMember(MemberID, ProjectID) values(2, 1)", objConn);
try
{
objCmd1.ExecuteNonQuery();
objCmd2.ExecuteNonQuery(); // 由于外键约束而引发异常
objTrans.Commit();
}
catch (Exception)
{
objTrans.Rollback();
}
finally
{
objConn.Close();
}
}
英文:
A user initiated transaction is a database transaction you or some code started using a SqlTransaction
. For example:
using (SqlConnection objConn = new SqlConnection(strConnString))
{
objConn.Open();
objTrans = objConn.BeginTransaction();
SqlCommand objCmd1 = new SqlCommand("insert into tblProject values(1, 'TestProject')", objConn);
SqlCommand objCmd2 = new SqlCommand("insert into tblProjectMember(MemberID, ProjectID) values(2, 1)", objConn);
try
{
objCmd1.ExecuteNonQuery();
objCmd2.ExecuteNonQuery(); // Throws exception due to foreign key constraint
objTrans.Commit();
}
catch (Exception)
{
objTrans.Rollback();
}
finally
{
objConn.Close();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论