views:

76

answers:

1

I am trying to update a record in my database using Linq to Sql. I'd like to update all fields, which will come from a new object, however if I try this:

originalObject = newObject
db.submitChanges()

It doesn't save the changes because it thinks the primary key has been changed (Or something along those lines.. it gives no error but doesn't update the object in the database)

I tried overwriting it like:

Dim originalKey = originalObject.MyPrimaryKey
originalObject = newObject
originalObject.MyPrimaryKey = originalKey
db.SubmitChanges()

...but that doesn't work either. If I set individual properties they will be saved (ie: originalObject.PropertyName = "New Value") then submitChanges, it works, but the object has about a hundred properties which I don't want to have to update each one individually. So, how can I update the object and submitChanges() successfully?

+1  A: 

If you already have the modified entity object, you should be able to use (yourDataContext).(yourTable).Attach(modifiedObject, true), followed by SubmitChanges.

nitzmahone