asp.net – Dapper.net交易问题

前端之家收集整理的这篇文章主要介绍了asp.net – Dapper.net交易问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将事务提交到我的sql Server 2008数据库 – 首先是2次插入然后是几次更新,但是,只要它尝试执行第一次更新,我就会收到以下错误

ExecuteNonQuery requires the command to have a transaction when the
connection assigned to the command is in a pending local transaction.
The Transaction property of the command has not been initialized.

这是代码,为简洁起见略有编辑:

using (_cn)
{
    _cn.Open();
    IDbTransaction transaction = _cn.BeginTransaction();
    topicId = (int)_cn.Query<decimal>(qAddTopic,new { pForumId = topic.ForumId },transaction).Single();
    postId = (int)_cn.Query<decimal>(qAddPost,new { pTopicId = topicId },transaction).Single();

    _cn.Execute(qUpdateForums,new { pLastPostId = postId });
    _cn.Execute((qUpdateSiteTotals));

    transaction.Commit();
}

前2个插入工作正常,但只要它尝试执行其中一个更新,就没有快乐.

解决方法

我发现了问题 – 当我调用更新时,我只是错过了事务参数,而之前的插入工作正常,我已经包含了IDbTransaction参数!我的错!

例:

Connection.Query<Entitiy>("sqlQuery",param: new { id= ID},transaction: Transaction)
原文链接:https://www.f2er.com/aspnet/247651.html

猜你在找的asp.Net相关文章