none
Timeout RRS feed

  • Question

  • I am using entity framework to update a large amout of data. I do it section by section in order to control memory usage as below. However, I got an {"The underlying provider failed on Open."} EntityException with the InnerException {"The operation is not valid for the state of the transaction."}. What is wrong with my code? I have set up the timeout as infinity (TimeSpan.Zero) and it failed in mid of the updating loop.

     TransactionOptions options = new TransactionOptions

    ();

    options.Timeout =

    TimeSpan

    .Zero;

     

    using (TransactionScope scope = new TransactionScope(TransactionScopeOption

    .RequiresNew, options))

    {

     

      using (UlssDbEntities db = new MyEntities

    (connectionString))

      {

        start a loop

          (1) load data from db and update data

          (2) call db.SaveChanges() to write to database

          (3) db.Detach(...) to release objects from entity framework

        repeat (1) - (3) for other object update

      }

      call scope.Complete() to commit transaction

    }

    


    Roy
    Thursday, September 15, 2011 2:00 PM

All replies

  • Hi,

    If you use TransactionScope, has you enabled the Distributed Transaction Coordinator (DTC)?

    Please, followe the steps described here in order to enable the DTC:

    Enable Network DTC Access for Windows Server 2008

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Thursday, September 15, 2011 2:33 PM
  • I am using it on local machine so I believe DTC is not needed in my case. Besides, I updated data section by section. It fails after I updated some of the sections (not committed of course). I just do not know why it generates the error and what the error means.
    Roy
    Thursday, September 15, 2011 3:40 PM
  • The exception was throw at step (3) in the loop where I try to release all the records that have been saved

    (3) db.Detach(...) to release objects from entity framework

    In my output window, it shows the following

    A first chance exception of type 'System.Transactions.TransactionException' occurred in System.Transactions.dll

    A first chance exception of type 'System.Transactions.TransactionException' occurred in System.Data.dll

    A first chance exception of type 'System.Data.EntityException' occurred in System.Data.Entity.dll


    Roy
    Thursday, September 15, 2011 4:11 PM
  • Hi again,

    Unfortunatelly who decides when a TransactionScope needs DTC is Entity Framework and it doesn't depends if you are in a local machine or not. 


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Thursday, September 15, 2011 4:16 PM
  • Hi Roy,

    Welcome!

    I think you can try to set EF command time out and Connection Time out in connection string.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Sunday, September 18, 2011 5:38 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, September 26, 2011 3:13 AM
    Moderator