none
Linq not updating table via "Attach" RRS feed

  • Question

  • I'm using the LINQ Attach method to try to update my table with a few values.  The code runs through as if everything processed fine however when I check my table, nothing was updated.

    Here's my code.  (consists of 2 methods.  Method 1 calls Method 2 via "saveorder"):

    METHOD 1:

    public void UpdateOrders()
        {
          foreach (Order order in _orderRepository.GetAllOrders())
          {
            char[] exported = "Y".ToCharArray();
            order.Exported = exported[0];
            order.Exported_date = DateTime.Now;
            _orderRepository.SaveOrder(order);
          }
        }

     

    -----------------------------------------

    METHOD 2:

     

    public void SaveOrder(Order order)
        {
          using(VictozaDataContext dc = _conn.GetContext())
          {
            order.UpdateDate = DateTime.Now;
            dc.Orders.Attach(order);
            dc.SubmitChanges();
          }
        }

    -------------------------------

    I verified my connection string and it's pointing to the propper database.  I checked my "order" collection just before calling "dc.SubmitChanges()" and it DOES in fact have all the correct updated values.  Any idea what I'm doing wrong to prevent it from updating the table?

    Thanks.

    Wednesday, April 21, 2010 7:47 PM

Answers

  • Here is an example of what I use:

     

    public bool Update(ref Employee empl, Common.ErrorHandler errorHandler)
        {
          
          bool success = true;
          empl.Signature = Common.Security.Signature();
    
          try
          {
            if (db.Employees.GetOriginalEntityState(empl) == null)
            {
              db.Employees.Attach(empl);
              db.Refresh(RefreshMode.KeepCurrentValues, empl);
            }
            db.SubmitChanges();
          }
          catch (Exception ex)
          {
            errorHandler("An error occured while updating an employee record. The error message is: " + ex.Message);
            success = false;
          }
          return (success);
        }

    Wednesday, April 21, 2010 11:26 PM

All replies

  • Here is an example of what I use:

     

    public bool Update(ref Employee empl, Common.ErrorHandler errorHandler)
        {
          
          bool success = true;
          empl.Signature = Common.Security.Signature();
    
          try
          {
            if (db.Employees.GetOriginalEntityState(empl) == null)
            {
              db.Employees.Attach(empl);
              db.Refresh(RefreshMode.KeepCurrentValues, empl);
            }
            db.SubmitChanges();
          }
          catch (Exception ex)
          {
            errorHandler("An error occured while updating an employee record. The error message is: " + ex.Message);
            success = false;
          }
          return (success);
        }

    Wednesday, April 21, 2010 11:26 PM
  • This line did the trick:

     db.Refresh(RefreshMode.KeepCurrentValues, empl);
    

    Thanks!

    Thursday, April 22, 2010 7:37 PM