实体框架:使用事务和回滚...可能? [英] Entity Framework: Using transactions and rollbacks... Possible?

查看:86
本文介绍了实体框架:使用事务和回滚...可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:(使用Sql 2005)

Issue: (With Sql 2005)


  • 如何在事务处理时查询数据库? (因为它锁定表)

  • 如何导致事务回滚,然后关闭自己以允许查询表?

  • How can I query the database while the transaction is up? (Since it locks the table)
  • How can cause a transaction to rollback and then close itself to allow the table to be queried?

所以我发现这很多:

[TestMethod]
public void CreateUser()
{
    TransactionScope transactionScope = new TransactionScope();

    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();

    DataContextHandler.Context.Dispose();
}

其中DataContextHandler只是一个简单的单例,暴露了我的实体的上下文对象。这似乎像你想的那样工作。它创建用户,保存,然后在程序结束时回滚。 (IE测试完成)

Where DataContextHandler is just a simple singleton that exposes the context object for my entities. This seems to work just as you would think. It creates the user, saves, then rolls back when the program ends. (IE test finishes)

问题:如何强制事务回滚并杀死自己,以便我可以查询表?

Problem: How do I force the transaction to rollback and kill itself so that I can query the table?

原因:为了测试目的,我想确保用户:

Reason: For testing purposes, I want to make sure the user:


  • 已保存

  • 可以正确查询以证明其存在

  • 已删除(垃圾数据)

  • 可以查询以确保已被删除。

  • Is Saved
  • Can be queried correctly to prove its existence
  • Is removed (Junk data)
  • Can be queried to make sure it was removed.

截至目前,只有当测试结束时,我才能让事务回滚,我无法弄清楚如何查询交易:

As of right now, I can only get the transaction to rollback if the test ends AND I can't figure out how to query with the transaction up:

[TestMethod]
public void CreateUser()
{
    ForumUser userToTest = new ForumUser();

    TransactionScope transactionScope = new TransactionScope();

    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();     

    Assert.IsTrue(userToTest.UserID > 0);

    var foundUser = (from user in DataContextHandler.Context.ForumUser
                    where user.UserID == userToTest.UserID
                    select user).Count();  //KABOOM Can't query since the 
                                           //transaction has the table locked.

    Assert.IsTrue(foundUser == 1);

    DataContextHandler.Context.Dispose();

    var after = (from user in DataContextHandler.Context.ForumUser
                 where user.UserID == userToTest.UserID
                 select user).Count(); //KABOOM Can't query since the 
                                       //transaction has the table locked.

    Assert.IsTrue(after == 0);
}

更新有效的回滚和检查,但仍然可以'在使用部分中查询:

using(TransactionScope transactionScope = new TransactionScope())
{
    DataContextHandler.Context.AddToForumUser(userToTest);
    DataContextHandler.Context.SaveChanges();
    Assert.IsTrue(userToTest.UserID > 0);
    //Still can't query here.

}

var after = (from user in DataContextHandler.Context.ForumUser
            where user.UserID == userToTest.UserID
            select user).Count();

Assert.IsTrue(after == 0);


推荐答案

MSDN ;


SaveChanges在事务中运行,如果任何脏的ObjectStateEntry对象不能被保存,SaveChanges将回滚该事务并抛出异常。

"SaveChanges operates within a transaction. SaveChanges will roll back that transaction and throw an exception if any of the dirty ObjectStateEntry objects cannot be persisted. "

所以似乎没有必要通过 TransactionScope 明确添加自己的事务处理。

So it seems that there is no need to to explicitly add your own transaction handling through TransactionScope.

这篇关于实体框架:使用事务和回滚...可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆