none
DbContext.ObjectContext.Refresh(RefreshMode.StoreWins, entry.Entity); Error RRS feed

  • Question

  • Hi all.

    I have a sql server compact 4 data base with a ConnctionString in my App.Config:

      <connectionStrings>
        <add name="AppNoThree.AppDbContext"
             connectionString="Data Source=D:\Learning\C# Core\Sessions\11thSession\SourceCode\AppNoOne\AppNoThree\Database.sdf"
             providerName="System.Data.SqlServerCE.4.0"/>
      </connectionStrings>
    </configuration>

    I have an Entity named Customer. When I update it with TransactionScope for check concurrency I get an error but when I do not use TransactionScope it work correctly. I want when concurrency occurred, changes will Refresh.

    What code should I add to it?

    Code without Exception:

                    Person person = context.Customers.Find(1);
                    person.FName = "John";

                    try
                    {
                        context.SaveChanges();
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        foreach (DbEntityEntry entry in ex.Entries)
                        {
                            context.ObjectContext.Refresh(RefreshMode.ClientWins, entry.Entity);
                        }
                        context.SaveChanges();
                        // ------------------------
                        throw;
                    }

    Code with EntityException:

                    Person person = context.Customers.Find(1);
                    person.FName = "John";

                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        try
                        {
                            context.SaveChanges();
                            transactionScope.Complete();
                        }
                        catch (DbUpdateConcurrencyException ex)
                        {
                            foreach (DbEntityEntry entry in ex.Entries)
                            {
                                context.ObjectContext.Refresh(RefreshMode.StoreWins, entry.Entity); // Error: The underlying provider failed on Open.
                            }
                            context.SaveChanges();
                            // ------------------------
                            throw;
                        }
                    }


    Each problem has a solution.



    • Edited by mammadkoma Saturday, November 24, 2012 11:41 AM
    Saturday, November 24, 2012 11:35 AM

Answers

  • You can't Open Connection to database while Transaction.Current.Sate == State.Aborted

    using (TransactionScope scope = new TransactionScope())

                    {
                        try
                        {
                            context.SaveChanges();
                            scope.Complete();
                        }
                        catch (DbUpdateConcurrencyException ex)
                        {
                            // You can't Open Connection to database while Transaction.Current.Sate == State.Aborted
                            scope.Dispose();
                            // So, Clear Current Transaction

                            context.InternalContext.Refresh(RefreshMode.StoreWins, ex.Entries.Select(entry => entry.Entity));

                            throw;
                        }
                    }

    What's the purpose of context.SaveChanges() after Refresh method ? I think you've made a mistake (-:

    • Marked as answer by mammadkoma Friday, November 30, 2012 9:17 PM
    Friday, November 30, 2012 6:41 PM

All replies

  • Hi,

    I found a similar thread. Please check whether you test your code on a Virtual machine: http://stackoverflow.com/questions/11743708/the-underlying-provider-failed-on-open

    Also, I recommend you try to open connection manually: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/3aa3942d-1b56-4030-8ea6-8d9734da4168/

    You could try the troubleshooting steps to find the root cause: http://social.msdn.microsoft.com/Forums/eu/adodotnetentityframework/thread/8c158185-3120-4d68-9489-d98d58f397a1

    Monday, November 26, 2012 2:26 PM
  • This code work in sql server correctly but get Exception in sql server compact !

    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        try
                        {
                            context.SaveChanges();
                            transactionScope.Complete();
                        }
                        catch (DbUpdateConcurrencyException ex)
                        {
                            foreach (DbEntityEntry entry in ex.Entries)
                            {
                                context.ObjectContext.Refresh(RefreshMode.StoreWins, entry.Entity); // Exception
                            }
                            context.SaveChanges();
                            // ------------------------
                            throw;
                        }
                    }


    Each problem has a solution.

    Tuesday, November 27, 2012 5:27 PM
  • You can't Open Connection to database while Transaction.Current.Sate == State.Aborted

    using (TransactionScope scope = new TransactionScope())

                    {
                        try
                        {
                            context.SaveChanges();
                            scope.Complete();
                        }
                        catch (DbUpdateConcurrencyException ex)
                        {
                            // You can't Open Connection to database while Transaction.Current.Sate == State.Aborted
                            scope.Dispose();
                            // So, Clear Current Transaction

                            context.InternalContext.Refresh(RefreshMode.StoreWins, ex.Entries.Select(entry => entry.Entity));

                            throw;
                        }
                    }

    What's the purpose of context.SaveChanges() after Refresh method ? I think you've made a mistake (-:

    • Marked as answer by mammadkoma Friday, November 30, 2012 9:17 PM
    Friday, November 30, 2012 6:41 PM