加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Dapper.net交易问题

发布时间:2020-12-16 04:14:31 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试将事务提交到我的Sql Server 2008数据库 – 首先是2次插入然后是几次更新,但是,只要它尝试执行第一次更新,我就会收到以下错误: ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in
我正在尝试将事务提交到我的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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读