English 中文(简体)
CE 4系统。 交易支助
原标题:SQL CE 4 System.Transaction support

还有一个类似的问题:here,但没有答案。

我正试图使用一个系统:交易,与EFCTP4和CEQ4的交流。

我为我的协会创造了以下交易归属感。 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();
        }
    }
}

When I use this filter I get the error:

系统. InvalidOperation 例外: 不能在交易范围内招募联系物体。

然而,以下测试通行证:

    [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的初衷有关?

问题回答

我也谈到这个问题。 我们不能使用与CE联线的交易。 最后,我的数据内容管理了我的切身关系,并落实了一个工作模式单位,以开展我的行动,然后在SqlCetransaction内执行所有预定的行动,然后呼吁作出承诺或退步。





相关问题
SQLServerCE Problem with parameterized queries from .NET

I am pulling the hair out of my head trying to figure this one out. I can t make Parameterized queries to work in VB.Net, when I am using parameters. From what I have found, using a parameter in a ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

热门标签