SQL CE 4 System.Transaction 支持

发布于 2024-10-03 02:28:28 字数 2198 浏览 2 评论 0原文

此处提出了类似的问题 但没有答案。

我正在尝试将 System.Transactions.CommittableTransaction 与 EF CTP4 和 SQL CE 4 结合使用。

我已为 ASP.NET MVC 控制器操作创建了以下事务属性:

public class TransactionAttribute : ActionFilterAttribute
{
    CommittableTransaction transaction;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        transaction = new CommittableTransaction();
        Transaction.Current = transaction;
        base.OnActionExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {           
        base.OnResultExecuted(filterContext);

        try
        {
            var isValid = filterContext.Exception == null || filterContext.ExceptionHandled;
            if (filterContext.Controller.ViewData.ModelState.IsValid && isValid) {
                transaction.Commit();
            } else {
                transaction.Rollback();
                Transaction.Current = null;
            }
        }
        finally
        {
            transaction.Dispose();
        }
    }
}

当我使用此过滤器时,出现错误:

System.InvalidOperationException:连接对象无法列入事务范围。

但是,以下测试通过了:

    [Test]
    public void Transaction_rolls_back_if_exception()
    {
        var transaction = new CommittableTransaction();
        Transaction.Current = transaction;

        try
        {
            var project = new Project { Title = "Test" };
            projectRepo.SaveOrUpdate(project);

            context.SaveChanges();

            var post = new Post { Title = "Some post" };
            blogRepo.SaveOrUpdate(post);

            throw new Exception();

            context.SaveChanges();

            transaction.Commit();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            Transaction.Current = null;
        }

        projectRepo.GetAll().Count().ShouldEqual(0);
        blogRepo.GetAll().Count().ShouldEqual(0);
    }

这与我初始化 DbContext 的方式有关吗?

A similar question was asked here but had no answer.

I am attempting to use a System.Transactions.CommittableTransaction with EF CTP4 and SQL CE 4.

I have created the following transaction attribute for my ASP.NET MVC Controller actions:

public class TransactionAttribute : ActionFilterAttribute
{
    CommittableTransaction transaction;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        transaction = new CommittableTransaction();
        Transaction.Current = transaction;
        base.OnActionExecuting(filterContext);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {           
        base.OnResultExecuted(filterContext);

        try
        {
            var isValid = filterContext.Exception == null || filterContext.ExceptionHandled;
            if (filterContext.Controller.ViewData.ModelState.IsValid && isValid) {
                transaction.Commit();
            } else {
                transaction.Rollback();
                Transaction.Current = null;
            }
        }
        finally
        {
            transaction.Dispose();
        }
    }
}

When I use this filter I get the error:

System.InvalidOperationException: The connection object can not be enlisted in transaction scope.

However, the following test passes:

    [Test]
    public void Transaction_rolls_back_if_exception()
    {
        var transaction = new CommittableTransaction();
        Transaction.Current = transaction;

        try
        {
            var project = new Project { Title = "Test" };
            projectRepo.SaveOrUpdate(project);

            context.SaveChanges();

            var post = new Post { Title = "Some post" };
            blogRepo.SaveOrUpdate(post);

            throw new Exception();

            context.SaveChanges();

            transaction.Commit();
        }
        catch (Exception ex)
        {
            transaction.Rollback();
            Transaction.Current = null;
        }

        projectRepo.GetAll().Count().ShouldEqual(0);
        blogRepo.GetAll().Count().ShouldEqual(0);
    }

Has this something to do with how I am initializing the DbContext?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-10-10 02:28:28

我遇到了同样的问题。您不能将事务与 CE 连接一起使用。我最终让我的数据上下文管理我的 ce 连接并实现一个工作单元模式来保存我的操作,然后执行 SqlCeTransaction 内的所有计划操作,然后自己调用提交或回滚。

http://msdn.microsoft.com/en-us/library/csz1c3h7.aspx

I ran into this same issue. You cannot use Transactions with the CE Connection. I ended up having my datacontext manage my ce connection and implementing a unit of work pattern to hold my actions, then executing all the scheduled actions inside a SqlCeTransaction then calling commit or rollback myself.

http://msdn.microsoft.com/en-us/library/csz1c3h7.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文