简体   繁体   English

使用事务通过实体框架回滚更改

[英]Using a transaction to Roll Back Changes with the Entity Framework

I have this specific DAO that inherits methods from a generic DAO I would like to add a transaction to this code in order to roll back all changes that where made if an exception is found 我有一个特定的DAO,它继承了通用DAO的方法,我想向该代码添加一个事务,以便回滚在发现异常的情况下所做的所有更改

        TDAO tDAO = new TDAO();
        TDAO2 tDAO2 = new TDAO2();


        //Get the DAO to delete from the database let's call them dDao and dDao2

        //Start the Transaction
        using (TransactionScope trans = new TransactionScope())
        {
            try
            {
                //Delete all SGC Associated switch
                TDAO2.Delete(dDao);

                //Delete switch
                TDAO.Delete(dDao2);

                //send notification
                base.SendChangeNotification();

                //Commit the Information Completing the Transaction
                trans.Complete();
            }
            catch (UpdateException ex)//SQL exception
            {
                //Log the error
                //Rollback the changes if there is an exception
                throw new Exception(Resources.ErrorMessages.OperationNotPermited);
            }
            catch (Exception ex) //Other exception
            {
                //Log the error
                throw new Exception(Resources.ErrorMessages.UndifenedError);
            }
        }

In Visual Studio, go to the References icon in your project. 在Visual Studio中,转到项目中的“引用”图标。 Right-click, Add Reference. 右键单击,添加引用。 Then search for System.Transactions.dll. 然后搜索System.Transactions.dll。 Select it, click Ok. 选择它,单击确定。 Then try rebuilding your project. 然后尝试重建您的项目。 Also make sure you have either a Using statement (C#) or Imports statement (VB) at the top, something like Using System.Transactions; 还要确保您在顶部有一个Using语句(C#)或Imports语句(VB),类似于Using System.Transactions;。

And the Changes are in the code . 更改在代码中。 Thank You 谢谢

You need to complete the transaction otherwise the transaction will be roll back. 您需要完成交易,否则交易将被回滚。 So, in your code you need to add Transaction.Complete() method, if you don't it will roll back itself. 因此,在您的代码中,您需要添加Transaction.Complete()方法,如果不这样做,它将自身回滚。

Although you marked this as solved, still an answer. 尽管您将此标记为已解决,但仍然是一个答案。

If you work with Entity Framework you should not worry about transactions. 如果使用Entity Framework,则不必担心事务。 The context manages the unit of work and sees to it that it is committed (or rolled back) in one transaction. 上下文管理工作单元,并确保它在一个事务中已提交(或回滚)。 Your code contains too much low-level data access stuff. 您的代码包含过多的低级数据访问内容。 Let EF do your CRUD actions. 让EF做您的CRUD动作。

Entity Framework allows you to be persistence ignorant in the most parts of your code. 实体框架使您可以在代码的大部分部分中保持持久性 My opinion is: you don't need a DAO pattern. 我的意见是:您不需要DAO模式。 Worse: it only gets in the way. 更糟糕的是:它只会阻碍您的前进。 It first separates database actions and context instances and then you have to put it all together by transaction scopes. 它首先将数据库操作和上下文实例分开,然后必须通过事务作用域将它们放在一起。 That means: you are managing the unit of work. 这意味着:您正在管理工作单元。 Work with POCO's, not with DAO's. 与POCO一起使用,而不与DAO一起使用。 Let one context instance track changes and save them by one SaveChanges() call. 让一个上下文实例跟踪更改并通过一个SaveChanges()调用将其保存。

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

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