none
DataContext.SubmitChanges does not update the database RRS feed

  • Question

  • I have a web project in VS 2010 and a silverlight project. I added a WCF service to provide data for the silverlight app. The service uses LinqToSQL to get/update data in a SQL2008 express database. The following code does not generate any sort of error, but the database is not changed either:

     

     

    using (Data.Context context = new Data.Context()) {
    context.Things.Attach(thing);
    context.SubmitChanges();
    }

    Tuesday, March 23, 2010 3:46 PM

All replies

  • Check if the 'primary key' attribute has been set in the class designer for the column(s) that make up the primary key. (Or if there is no primary key, for one or several columns that together will uniquely identify a record). L2S uses the 'primary key' attribute when updating/deleting records...
    Kristofer - Huagati Systems Co., Ltd.
    Cool tools for Linq-to-SQL and Entity Framework:
    huagati.com/dbmltools (add-in with new features for the L2S and EF designers in VS2008 and VS2010)
    huagati.com/L2SProfiler (Query profiler for Linq-to-SQL and LLBLGen Pro)
    Wednesday, March 24, 2010 7:32 AM
    Answerer
  • Hi Wil,

    The attach method overload you used is intended to attach "original" objects followed by the modifications.

    context.Things.Attach(thing)
    thing.SomeProperty="New Value"
    ..
    context.SubmitChanges()

     

    If have are receiving modified objects in your WCF service, you will need to send the "original" as well as "modified" object and call this overload. However, if you have a timestamp column in your database, you can simply attach the "modified" object with this overload as: context.Things.Attach(modified, true) .

    Regards,


    Syed Mehroz Alam
    My Blog | My Articles
    Wednesday, March 24, 2010 10:21 AM
  • Well I can't carry around the original, this is for a WCF service. Is there no other way to do this without a timestamp field?
    Thursday, March 25, 2010 12:25 PM
  • Wil,

    This may not be simple and you will have to sacrifice the concurrency and conflict resolutions provided by LINQ To SQL. I have not tested it but may you be able to achieve this by setting UpdateCheck =Never for all the fields except the primary key and attaching a self-constructed data object with just the primary key filled as the original one.

    //create a dummy object as original with just the primary key field
    DataObject original = new DataObject() { ID=modifedObj.ID };
    
    
    //attach both the orignal and the modifed obj
    context.Attach( modified, original );

    Regards,

    Syed Mehroz Alam


    Syed Mehroz Alam
    My Blog | My Articles
    Friday, March 26, 2010 6:11 AM