Rollback using Entity framework

Last post 07-09-2009 9:04 AM by zeeshanfawad. 2 replies.

Sort Posts:

  • Rollback using Entity framework

    06-16-2009, 6:25 AM
    • Member
      5 point Member
    • zeeshanfawad
    • Member since 07-11-2007, 1:09 PM
    • Lahore
    • Posts 47

     Hello Friends,
    How are you?? I am facing problem in ADO.NET entity framework. I want to impliment Rollback functionality using Entity frameowrk. I am trying to insert record in multiple tables in one transaction using  different contexts. My application scenario is given below:

    EntityContainer ec = new EntityContainer();
    //Make Entity Object
    //Set all values of Entity and Call ec.AddToEntity() object
    //Call: ec.SaveChanges();
    //Make New Method for Other Entity
        --Make New Entity Container object by name ec2.
        --Set all values of Entity
        --and then SaveChanges of ec2
    Now i've problem when ec1 successfully updated the value and if ec2 fails to Save values in table. Then all values from ec1 should also be Rollback. I am try to solve it by using "TransactionScope()" but it faiuls to commit changes. Code snippest is given below:
                              {
                                  using (TransactionScope ts = new TransactionScope())
                                    {

                                        ec1.AddToEntity(newObj);
                                        ec1.SaveChanges();                                                                    
                                        AddEntity2();
                                        ts.Complete();
                                    }
                            }

    AddEntity2()
    {
               //Code for ec2 operations

               ec2.SaveChanges();
    }

    When it call ts.complete then it gives exception that: "The transaction has aborted".

    Can anyone help us that how i can solve this issue??

    Zeeshan
  • Re: Rollback using Entity framework

    06-16-2009, 11:40 AM
    • Star
      12,651 point Star
    • docluv
    • Member since 06-29-2002, 11:16 PM
    • Willow Spring NC
    • Posts 2,003
    • TrustedFriends-MVPs
    Transactions are automatically managed within the same object context by the Entity Framework Object Services. This includes multiple operations that are dependent on successful completion of queries or transactions that depend on distributed systems, such as e-mail or MSMQ. Entity Framework operations can be executed within a System.Transactions Transaction to ensure the requirements are met.The use of transactions and Object Services requires the following considerations: only operations against the data source are transacted, SaveChanges will use any existing transaction to perform the operation, if none exist it will create one. Changes to objects in the object context are not accepted until the entire transaction is complete. If you retry an operation within a transaction the SaveChanges method should be called with a acceptChangesDuringSave set to false, then call AcceptAllChanges after the transaction operations have completed. Avoid calling SaveChanges after calling AcceptAllChanges, this will cause the context to reapply all the changes to the data source.

     

  • Re: Rollback using Entity framework

    07-09-2009, 9:04 AM
    • Member
      5 point Member
    • zeeshanfawad
    • Member since 07-11-2007, 1:09 PM
    • Lahore
    • Posts 47

    Hello Sir,

    I've solved rollback issue. But i am getting another exception while making transactions on Network. Description is given below:

    "
    I am finding a problem in Transaction scope  using entity framework. I've also started DTC services in windows services. It gives me Exception that "The underlying provider failed on Open.".  My inner exception detail is given below:

    {"Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool."}

    {"The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)"}

    When i deploy my database on my own client machine it works properly. But when i deploy my data base on another machine. Then Transactionscope gives me error. Can anyone help me regarding this issue. Its very urgent. Waiting for quick response."

    Zeeshan
Page 1 of 1 (3 items)