none
Linq Update RRS feed

  • Question

  • Hello

    I wanna know any way i could update records using linq without setting each value. Here's what i was doing:

    public void UpdateUser(User updateuser)
    {
       using (var ctx = new MyDataContext())
       {
          var user = ctx.User.Where(p => p.id.Equals(updateuser.id);

    user.name = updateuser.name;
    //other updates
      ctx.SubmitChanges();
    } }

    After some research this i'm doing:

    public void UpdateUser(User updateuser)
    {
       using (var ctx = new MyDataContext())
       { 
          ctx.User.Attach(updateuser,GetUserById(user.id));
          ctx.SubmitChanges();
       }
    }
    
    This is working but it updates ALL VALUES. I wanted to update just the changed values. i Tried to add this line :

    ctx.Refresh(RefreshMode.KeepChanges, updateuser);

    but didn't work at all.

    Does someone have a solution for this problem?

    Thanks
    Friday, February 19, 2010 2:07 PM

Answers

  • If you just want to update user name alone, then just include the required columns into this DataContext. Now call the submit changes method.
    By this way the unnecessary column can be isolated from updates.
     
    - Ananth Ramasamy Meenachi www.msarm.com
    "lukazbcc" wrote in message news:5fa6c467-823b-4da6-a6ab-4e299aa467bd...
    Hello

    I wanna know any way i could update records using linq without setting each value. Here's what i was doing:

    public void UpdateUser(User updateuser)
    
    {
    
       using (var ctx = new MyDataContext())
    
       {
    
          var user = ctx.User.Where(p => p.id.Equals(updateuser.id);

    user.name = updateuser.name;
    //other updates
      ctx.SubmitChanges();
    } }

    After some research this i'm doing:

    public void UpdateUser(User updateuser)
    
    {
    
       using (var ctx = new MyDataContext())
    
       { 
    
          ctx.User.Attach(updateuser,GetUserById(user.id));
    
          ctx.SubmitChanges();
    
       }
    
    }
    
    
    This is working but it updates ALL VALUES. I wanted to update just the changed values. i Tried to add this line :

    ctx.Refresh(RefreshMode.KeepChanges, updateuser);

    but didn't work at all.

    Does someone have a solution for this problem?

    Thanks

    --- Hope this helps. Ananth Ramasamy Meenachi https://www.msarm.com
    Monday, February 22, 2010 6:33 PM