0

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

        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. Right-click, Add Reference. Then search for 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;

And the Changes are in the code . Thank You

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
rjcossa
  • 117
  • 4
  • 18
  • 1
    What is it you want to rollback? Changes to the DB? or changes to the data-context? For changes to the DB - just use a regular database transaction. For changes to the data-context - don't: just *throw it away*. – Marc Gravell Nov 06 '12 at 08:19
  • I want to roll back changes to the DB . How can i do that ? – rjcossa Nov 06 '12 at 08:26
  • Oh and just for additional info , Database management system is SQL Server 2008 – rjcossa Nov 06 '12 at 08:44
  • I think it's better to leave the original question and add you solution as an answer below. Then accept you answer as correct. It will mark question as answered and other users will see problem AND solution. – Sergey Berezovskiy Nov 06 '12 at 09:40
  • User, you are using StackOverflow in the wrong way. Question should remain as question, and answer should be provided as answer. – Dialecticus Nov 06 '12 at 09:43
  • Please do not edit "Solved" into your question, nor insert your answer. You can post an answer yourself, and accept it, to reflect that you have an answer. – Andrew Barber May 22 '13 at 12:47

2 Answers2

0

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.

0

Although you marked this as solved, still an answer.

If you work with Entity Framework you should not worry about transactions. 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.

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. 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. Let one context instance track changes and save them by one SaveChanges() call.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291