简体   繁体   English

实体框架6.1中的事务范围回滚

[英]Transaction scope rollback in Entity Framework 6.1

I have a problem with TransactionScope rollback. 我对TransactionScope回滚有问题。

When rolling back the TransactionScope state of objects in context (changetracker) changes from add to modify 当回滚上下文(changetracker)中对象的TransactionScope状态时,从添加更改为修改

private xType _x 
{
    get;
    set
}

public void add(x: xtype) 
{
    context.xlist.add(x);
}

public xclass: object 
{
    public xclass() 
    {
        _x = new xtype();
        add(_x);
    }

    public void savechanges; 
    {
        using(transactionscope = new transactionscope()) 
        {
            try 
            {
                context.savechanges();
                x = 0;
                x = x / x;
                transcope.complete();
            } catch 
            {
                transcope.rollback();
            }
        }
    }

Project uses .net 4.0, vs 2013, ef 6.1, SQL Server 2008 Express, and uses UnitOfWork, Repository patterns 项目使用.net 4.0和2013,ef 6.1,SQL Server 2008 Express,并使用UnitOfWork,存储库模式

You are modifying your objects after having saved them. 保存对象后,您将对其进行修改。 That makes them modified. 那使他们修改了。

EF does nothing special when a transaction rolls back. 事务回滚时,EF不会执行任何特殊操作。 None of its internal state changes. 其内部状态没有变化。 Entity objects are not reverted back. 实体对象不会还原。 The best pattern is to abandon the EF context after a rollback. 最好的模式是回滚后放弃EF上下文。

You do not need to explicitly rollback. 您不需要显式回滚。 Delete the catch. 删除渔获物。 Right now all that it accomplishes is to swallow all errors. 现在,它所完成的只是吞没所有错误。

Please do not use transactionscope this way. 请不要以这种方式使用transactionscope。 Transaction scope is a scope. 交易范围是一个范围。 You should do something with your database withing the using clause where your create the transaction scope. 您应该在创建事务范围的地方使用using子句对数据库进行某些操作。 What people do a lot is, after applying changes, commit them on them by calling the commit method. 人们所做的很多事情是,在应用更改后,通过调用commit方法将更改提交给他们。 If there is a crash, nothing is committed and transaction scope knows that the changes so far require a rollback. 如果发生崩溃,则不会提交任何内容,并且事务范围知道到目前为止的更改需要回滚。 Transaction scope can be used to implement the "unit of work" design pattern. 事务范围可用于实现“工作单元”设计模式。 Things within the using clause are considered to be one unit of work. using子句中的内容被视为一个工作单元。 They should be done all (or not at all). 它们应该全部完成(或完全不做)。 Therefore, some kind of rollback is needed when there is a crash before calling commit. 因此,在调用commit之前发生崩溃时,需要某种回滚。 Doing things almost completely is not accepted, since it is one unit of work. 几乎完全做事是不被接受的,因为这是一项工作。

Here is some code example that explains the basic concept. 这是一些代码示例,解释了基本概念。 https://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.transactions.transactionscope(v=vs.110).aspx

Here are more examples: http://www.codeproject.com/Articles/690136/All-About-TransactionScope 这里有更多示例: http : //www.codeproject.com/Articles/690136/All-About-TransactionScope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM