1

当我对 SqlServer CE 4.0 和 TransactionScope 使用它时,我有一些代码会引发异常。首先是代码,然后是错误。

// Arrange.
// ... some stuff ...
// like .. order = get order with ID #1.

// Act.
using (new TransactionScope())
{
    order.Name = name; // Update a field.
    _orderRepository.Save(order);
    _unitOfWork.Commit(); // <-- this works 100% fine.

    // Assert.
    // Reload the order so we can see if the data persisted to the DB.
    var updatedOrder = _orderRepository
    .Find()
    .Where(x => x.OrderId == 1)
    .SingleOrDefault(); <-- // this throws the exception.

    Assert.IsNotNull(updatedOrder);
    Assert.AreEqual(name, order.Name);
}

异常错误是:-

System.Data.EntityException:基础提供程序在打开时失败。---> System.InvalidOperationException:无法将连接对象纳入事务范围。

所以第一次保存/提交工作正常,但是当我尝试再次检索对象(查看数据是否保留在事务中)时,就会发生错误。

现在我确定这是一个单一事务而不是分布式事务......所以我假设这应该可以工作?

建议,好心人?

4

1 回答 1

0

我不得不与之抗争,这是可能的解决方案:http: //fknet.wordpress.com/2011/02/25/entity-framework-problemsolution-of-default-connection-closing/

于 2011-02-25T23:08:03.523 回答