none
Entity Framework - How to call ObjectContext.Refresh() in a separate method by passing in table name RRS feed

  • Question

  • I am trying to move this:

    database.Refresh(RefreshMode.StoreWins, database.MyTable);

    into a separate method (so I can call it for any specified table and put a try/catch around it for some common exceptions when the network connection to the MSSQL database server is unavailable.):

    TableRefresh(database.MyTable);

    However, when I do this, I get "InvalidOperationException was unhandled."

    I have tried declaring the method in 2 different ways, but neither works:

    private void TableRefresh(object table)
    {
      try
      {
          database.Refresh(RefreshMode.StoreWins, table);
      }
      catch (System.Data.EntityCommandExecutionException ex)
      {
         <error processing code here>
      }
    }
    
    
    private void TableRefresh<T>(<T> table)
    {
      try
      {
          database.Refresh(RefreshMode.StoreWins, table);
      }
      catch (System.Data.EntityCommandExecutionException ex)
      {
         <error processing code here>
      }
    }
    


    Please note that the problem is not that I need to catch InvalidOperationException, but that I cannot move the "database.Refresh" to a separate method.

    Thanks for any assistance.

    Tuesday, September 6, 2011 5:42 AM

All replies

  • Hello rusitzky,

    Thanks for your post.

    As far as I know, the ObjectContext.Refresh() method is to some ObjectContext instance which you use. You could use it in your Using block which you have already created an ObjectContext instance. If you move it to a separate method, the method doesn't which ObjectContext instance you want to refresh if you have more than one ObjectContext instance.

    Please read the following code:

    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        try
        {
            // Perform an operation with a high-level of concurrency.
            // Change the status of all orders without an approval code.
            ObjectQuery<SalesOrderHeader> orders =
                context.SalesOrderHeaders.Where(
                "it.CreditCardApprovalCode IS NULL").Top("100");

            foreach (SalesOrderHeader order in orders)
            {
                // Reset the order status to 4 = Rejected.
                order.Status = 4;
            }
            try
            {
                // Try to save changes, which may cause a conflict.
                int num = context.SaveChanges();
                Console.WriteLine("No conflicts. " +
                    num.ToString() + " updates saved.");
            }
            catch (OptimisticConcurrencyException)
            {
                // Resolve the concurrency conflict by refreshing the
                // object context before re-saving changes. 
                context.Refresh(RefreshMode.ClientWins, orders);

                // Save changes.
                context.SaveChanges();
                Console.WriteLine("OptimisticConcurrencyException "
                + "handled and changes saved");
            }

            foreach (SalesOrderHeader order in orders)
            {
                Console.WriteLine("Order ID: " + order.SalesOrderID.ToString()
                    + " Order status: " + order.Status.ToString());
            }
        }
        catch (UpdateException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

    I hope this can help you.

     

    have a nice day,


    Jackie Sun [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.

    Thursday, September 8, 2011 8:01 AM
    Moderator
  • Jackie,

      Thanks very much for the reply, but perhaps my question wasn't well worded.  The code that currently works is:

                try
                {
                    database.Refresh(RefreshMode.StoreWins, database.Customers);
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    this.errorTextBox.AppendText(DateTime.Now + ":   EXCEPTION db.Refresh 'Customers': ex: " + ex.Message + Environment.NewLine);
                    SendAlertEmail("Exception: " + ex.Message);
                    Thread.Sleep(60);
                }
                catch (System.Data.EntityCommandExecutionException ex)
                {
                    this.errorTextBox.AppendText(DateTime.Now + ":   EXCEPTION db.Refresh 'Customers': ex: " + ex.Message + Environment.NewLine);
                    SendAlertEmail("Exception: " + ex.Message);
                    Thread.Sleep(60);
                }
                catch (System.Data.EntityException ex)
                {
                    this.errorTextBox.AppendText(DateTime.Now + ":   EXCEPTION db.Refresh 'Customers': ex: " + ex.Message + Environment.NewLine);
                    SendAlertEmail("Exception: " + ex.Message);
                    Thread.Sleep(60);
                }
    


    What I want to do is to change the code to being called like this:

    TableRefresh(database.Customers);


    with a method like this:

    private void TableRefresh(object table)
    {
        var name = table.ToString();
    
        try
        {
            database.Refresh(RefreshMode.StoreWins, table);
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            this.errorTextBox.AppendText(DateTime.Now + ":   EXCEPTION db.Refresh '" + name + "': ex: " + ex.Message + Environment.NewLine);
            SendAlertEmail("Exception: " + ex.Message);
            Thread.Sleep(60);
         }
         catch (System.Data.EntityCommandExecutionException ex)
         {
             this.errorTextBox.AppendText(DateTime.Now + ":   EXCEPTION db.Refresh '" + name + "': ex: " + ex.Message + Environment.NewLine);
             SendAlertEmail("Exception: " + ex.Message);
             Thread.Sleep(60);
         }
         catch (System.Data.EntityException ex)
         {
             this.errorTextBox.AppendText(DateTime.Now + ":   EXCEPTION db.Refresh '" + name + "': ex: " + ex.Message + Environment.NewLine);
             SendAlertEmail("Exception: " + ex.Message);
             Thread.Sleep(60);
         }
    }

    The real issue is why can't I pass the table name into a method and call Refresh() on it?  Thanks.

    Thursday, September 8, 2011 1:51 PM
  • Jackie,

      Could you please respond to my last post?  Thanks.

    Monday, September 19, 2011 6:12 PM