0

To insert a record in SQL Server CE, you call ..InsertOnSubmit(), passing the class instance that represents the record being inserted, followed by .SubmitChanges();

Do I need a similar construct for updating a record? I've got this:

SQLCEDataContext sqlcedc = new SQLCEDataContext(SQLCEDataContext.DBConnectionString);
var invitations = (from SQLCEDataDefinition invitation in
                       sqlcedc.SQLCEDataDefinitions
                   where invitation.SenderID == senderID
                   select invitation).SingleOrDefault();
invitations.SenderDeviceID = senderDeviceID;
sqlcedc.SubmitChanges();

..but wonder if I need the analogue of InsertOnSubmit() - but there is no UpdateOnSubmit() that I can see. Do I need to user InsertOnSubmit() even though this is an Update operation,not an Insert operation?

windows-phone-8 sql-server-ce update linq

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

2 Answers2

2

No you don't have an UpdateOnSubmit() and you can't use the InsertOnSubmit() to update your record.

You need to fetch the data first, update your property and then use the SubmitChanges()

Fabrice
  • 921
  • 1
  • 6
  • 11
  • IOW - just like I'm doing it, then, right? It seems kind of f strange that there's an InsertOnSubmit() if it's not needed when updating. – B. Clay Shannon-B. Crow Raven Jan 09 '13 at 19:07
  • Yes it's right. In fact LINQ to SQL does automatic change tracking, so it's already aware of changes you've made to objects you've retrieved via the DataContext. A request to update the object during SubmitChanges is implied. – Fabrice Jan 09 '13 at 19:14
  • There is a workaround. See my answer [here](http://stackoverflow.com/a/40754837/450913). – orad Nov 23 '16 at 02:05
1

Try like this:

   SQLCEDataContext sqlcedc = new SQLCEDataContext(SQLCEDataContext.DBConnectionString);
var invitations = (from SQLCEDataDefinition invitation in
                       sqlcedc.SQLCEDataDefinitions
                   where invitation.SenderID == senderID
                   select invitation).SingleOrDefault();
invitations.SenderDeviceID = senderDeviceID;
sqlcedc.SubmitChanges();
Norbert Pisz
  • 3,392
  • 3
  • 27
  • 42