none
How to perform multiple Updates in entity framework RRS feed

  • Question

  • Hi,

    I am trying to updates multiple records at the same time using savechanges but it updates all the records in the table with the last value. can you please tell me how to perform updates multiples records in the table at a same time instead of calling savechanges every time. I have to updates 12 records and i wish to do it in one savechanges call instead of calling 12 savechagnes() in entity framework

     

    I am using Visual Studio 2010 with C#.

     

    Thanks 

    Monday, August 1, 2011 7:23 AM

Answers

  • Hi,

    If i understood your question well, following could be an answer for you..

    TestEntities context = new TestEntities();
    
      context.UserInfo.First(r => r.UserId == 12).UserName = "User One";
    
      context.UserInfo.First(r => r.UserId == 1111).UserName = "User Two";
    
      context.SaveChanges();

    Here i do save changes after doing two updates.

     



    Monday, August 1, 2011 1:15 PM
  • Hi Pankaj;

    The Entity Framework ObjectContext keeps track of all entities that have been added to it. If you query the database and it returns record, those records are added to the ObjectContext as unchanged. When you make a change to any of those records or add a new entities or delete any entities the ObjectContext places a duplicate records into a new collection one for each modified, inserted, or deleted entity. When you execute the SaveChanges method, the Entity Framework only sends SQL commands for those records that are in the modified, insert, and deleted collection and no others.

    So in order to see what is happening please post the code so that we can make some recommendations.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, August 1, 2011 3:08 PM

All replies

  • Hi,

    If i understood your question well, following could be an answer for you..

    TestEntities context = new TestEntities();
    
      context.UserInfo.First(r => r.UserId == 12).UserName = "User One";
    
      context.UserInfo.First(r => r.UserId == 1111).UserName = "User Two";
    
      context.SaveChanges();

    Here i do save changes after doing two updates.

     



    Monday, August 1, 2011 1:15 PM
  • Hi Pankaj;

    The Entity Framework ObjectContext keeps track of all entities that have been added to it. If you query the database and it returns record, those records are added to the ObjectContext as unchanged. When you make a change to any of those records or add a new entities or delete any entities the ObjectContext places a duplicate records into a new collection one for each modified, inserted, or deleted entity. When you execute the SaveChanges method, the Entity Framework only sends SQL commands for those records that are in the modified, insert, and deleted collection and no others.

    So in order to see what is happening please post the code so that we can make some recommendations.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, August 1, 2011 3:08 PM
  • Thanks Fernando and muthukumar.

    Issue has been resolved !.

     

    Thanks again for your support.

    Tuesday, August 2, 2011 9:06 AM
  • Not a problem Pankaj, glad to be of some help.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, August 2, 2011 3:20 PM
  • You can use this library

    https://github.com/loresoft/EntityFramework.Extended

    Friday, February 15, 2013 2:24 PM