none
How to do an Update in LINQ RRS feed

  • Question

  • I have been, off and on, trying to get Update to work in LINQ for some time now but have always been able to get away with Delete and Add (which is very easy).  Now I have a case where I must somehow get it to work.  I have googled everywhere.  Below I have pasted what I am doing.  TeeID is the Identity key to the record.  I am just trying to change the TeeName (for now).  I have checked the TeeID and the TeeName and they contain the proper values.  The SubmitChanges does not get an error.  It just has no effect, the TeeName is unchanged.  What am I doing wrong?

    Thanks, Gary

     

    var myCTTR = (from C in db.CourseTeesTotals

     

    where C.TeeID == myClass.CourseTeeTotalRec.TeeID

     

    select C).Single();

    myCTTR.TeeName = myClass.CourseTeeTotalRec.TeeName;

    db.SubmitChanges();


    .net Developer
    Thursday, May 27, 2010 5:20 PM

Answers

  • Turns out my post was bogus.  The reason the code at the beginning of this thread was not having any effect is because it was inside a Transactionscope and I faild to do the commit.  Once I do the commit it works fine so that can stand as an example of how to do an Update in LINQ.

    Sorry for the distraction.

    Gary


    .net Developer
    • Marked as answer by Dean Blakely Thursday, May 27, 2010 9:50 PM
    Thursday, May 27, 2010 9:50 PM

All replies

  • There are two different types of updates attached and disconnected.  I wrote the following code from the top of my head, so I don't know if it's all correct, but you can get the idea from it.

    If you get data from a datacontext, you can just change the values, and submit changes.  Otherwise you have to attach the object to the datacontext.  If you attach an item that is already from the datacontext it throws an exception.

    public class ItemBLL
    {
     private DataContext localdatacontext = new DataConext();
    
     public List<Item> GetData()
     {
      return (from item in localdatacontext.Table<Item>
       select item).ToList();
     }
    
     public void AttachedUpdate(Item item)
     {
      localdatacontext.SubmitChanges();
     }
    
     public void DisconnectedUpdate(Item item)
     {
      localdatacontext.Attach(item);
      localdatacontext.SubmitChanges();
     }
    
     public void Update(Item item)
     {
      try
      {
       DisconnectedUpdate(item);
      }
      catch(//Find Exception type)
      {
       AttachedUpdate(item);
      }
     }
    }
    
    
    
    Thursday, May 27, 2010 5:38 PM
  • Sorry, but I couldn't get anything working from what you have.  Seems what I am trying to do is super simple.  I find my example all over as an answer to the question of how to update using LINQ (and the question is asked a lot and I see why)  but it doesn't work.  Have you any Idea why my code is not working?

    tia


    .net Developer
    Thursday, May 27, 2010 9:23 PM
  • Turns out my post was bogus.  The reason the code at the beginning of this thread was not having any effect is because it was inside a Transactionscope and I faild to do the commit.  Once I do the commit it works fine so that can stand as an example of how to do an Update in LINQ.

    Sorry for the distraction.

    Gary


    .net Developer
    • Marked as answer by Dean Blakely Thursday, May 27, 2010 9:50 PM
    Thursday, May 27, 2010 9:50 PM