I have a Linq-To-SQL object obj
of type MyClass
that I have loaded via my data context.
Now I want to force that object to save, even if no fields have actually changed, so that the save action can set off some triggers behind the scenes.
What's the easiest way of making my data context think that obj
is dirty, so that a call to SubmitChanges()
will cause obj
to be saved?
Just change a property to a dummy value and then back...
Edit: let me take that back. The L2S change tracker won't be fooled by that. The easiest/cleanest/safest way if you don't want to change any of the existing columns is probably to add a new column and change that.
If you absolutely can't make any db changes (i.e. add a new column), then going at the change tracker with reflection might be an option. I haven't tried it, but it looks like the route to that would be (roughly):
1) the datacontext has a private member called services.
2) services points to a CommonDataServices which has a private member tracker and an internal member ChangeTracker (returning the former).
3) Change trackers has a GetTrackedObject internal method that returns a TrackedObject.
4) TrackedObject has a ConvertToModified method...
Edit #2: I just tested the reflection route above and it seems to work. E.g.:
...and the implementation of MakeDirty is:
You could try 2 submits if that's not going to break anything else? Therefore you can just use a variation of Kristofer's first answer:
Any good for your purposes?