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

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

问题描述

问题:(使用 Sql 2005)

Issue: (With Sql 2005)

  • 如何在事务完成时查询数据库?(因为它锁定了表)
  • 如何使事务回滚然后关闭自身以允许查询表?

所以我发现了这么多:

[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:

  • 已保存
  • 可以正确查询以证明其存在
  • 被删除(垃圾数据)
  • 可以查询以确保它已被删除.

截至目前,如果测试结束并且我无法弄清楚如何在事务启动时进行查询,我只能让事务回滚:

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);
}

UPDATE 这用于回滚和检查,但仍然无法在 using 部分中查询:

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天全站免登陆