none
Update Record RRS feed

  • Question

  • <pre>   DocentDataDataContext con = new DocentDataDataContext();
    
       var sub = (from su in con.ScheduledEvents
    
          where update.eventId == su.EventId
    
          select su).FirstOrDefault();
    
       sub.TimeStart = update.startTime;
    
       sub.TimeEnd = update.endTime;
    
       con.SubmitChanges();
    
    
    
    

    How do I update a record in LINQ without enumerating all the field assignments? My "update" record is the same structure as the record being updated and I have about 40-50 fields.

    I've seen similar posts on this subject but none recently and I'm having trouble getting it to work.

    Thanks,

    Paul P.


    Sunday, April 3, 2011 1:43 PM

Answers

  • Hello Paul,

     

    Welcome to the LINQ to SQL Forum!

    As I know, if you want to update an object you have to retrieve the object first and then modify its fields, or use the Attach method if you have an object instance that did not originate from the DataContext.  If you use the Attach method you must either supply the object in its before edit state and after edit state (two instances) or use the 'as-modified' option (yet only if your object has a version/timestamp field). Once the object(s) are attached they are part of the DataContext you attached them to and should not be used with any other DataContext instance.

    Another, you can use DatraContext.ExecuteCommand() method, which executes SQL commands directly on the database. More information about it, please read this :

    http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.executecommand.aspx

    I hope this can help you.

     

    Good 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.


    Tuesday, April 5, 2011 8:08 AM
    Moderator

All replies

  • Hello Paul,

     

    Welcome to the LINQ to SQL Forum!

    As I know, if you want to update an object you have to retrieve the object first and then modify its fields, or use the Attach method if you have an object instance that did not originate from the DataContext.  If you use the Attach method you must either supply the object in its before edit state and after edit state (two instances) or use the 'as-modified' option (yet only if your object has a version/timestamp field). Once the object(s) are attached they are part of the DataContext you attached them to and should not be used with any other DataContext instance.

    Another, you can use DatraContext.ExecuteCommand() method, which executes SQL commands directly on the database. More information about it, please read this :

    http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.executecommand.aspx

    I hope this can help you.

     

    Good 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.


    Tuesday, April 5, 2011 8:08 AM
    Moderator
  • Hello ZooGuy1492,

    Linq do not provide mass updates.

    If you want to do mass updates you should use SQLCommand.

    Kind regards,


    aelassas.free.fr
    Saturday, April 16, 2011 8:50 PM