【问题标题】:TransactionScope, where is begin transaction on sql profiler?TransactionScope,sql profiler 上的开始事务在哪里?
【发布时间】:2011-07-09 14:45:32
【问题描述】:

我需要在事务上下文中做这样的事情

using(var context = new Ctx())
{

using (TransactionScope tran = new TransactionScope())
{
    decimal debit = 10M;
    int id = 1;

    var data = context.Cashier
        .Where(w => w.ID == id)
        .Select(s => new{ s.Money })
        .Single();


    Cashier cashier = new Cashier(){ ID = id };
    context.Cashier.Attach(cashier);

    cashier.Money = data.Money - debit;
    context.Entry(cashier).Property(p => p.Money ).IsModified = true;

    context.SaveChanges(SaveOptions.None);
    tran.Complete();
}
}

我正在运行 sql profiler 但看不到 begin tran,那段代码是否正确?我错过了什么吗?

【问题讨论】:

  • 可能你已经过滤掉了?您也可以尝试在事务中创建 ctx,只是为了看看 I 会改变什么。
  • 为什么要为这段代码使用事务范围? SaveChanges 如果您未定义它,则在内部使用事务,因此除非您使用多个事务资源或多次调用 SaveChanges,否则您不需要它。
  • Ladislav,这是为了防止数据错误,别人可以用其他方法修改收银员的钱,如果不是在交易中,这个信息就会出错
  • @Alexandre:@Ladislav 是正确的; SaveChanges 函数在保存时始终使用事务。除非您多次调用SaveChanges(或在此代码块中执行其他数据库操作),否则不需要其他事务。
  • 我理解Adam,但是实体框架的开始tran只有在调用SaveChanges时才会发生,我需要在此之前的事务,更具体地说: context.Cashier .Where(w => w.ID = = id) .Select(s => new{ s.Money }) .Single();

标签: c# entity-framework entity-framework-4.1 transactionscope


【解决方案1】:

就像@Marc 在他的评论中所说,这些消息可能被过滤掉了。您只会在默认配置文件中获取 T-SQL 事务消息,而不是直接使用 API 发送的事务消息(如 TransactionScope 所做的那样)。

在 SQL Server Profiler 中,转到跟踪事件选择并选中“显示所有事件”复选框。底部是“交易”类别,它应该为您提供所需的内容。具体来说,以TM:开头的事件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 2013-08-02
    相关资源
    最近更新 更多