I have a bunch of tables that have DateUpdated columns.
How can I have those fields automatically set to DateTime.Now
when the objects are persisted back to the data store when SaveChanges()
is called.
I don't need to do it across the board with one piece of code. I would be OK with adding event handlers in all of the partial classes, but I didn't see anything I could hook into. And I'd rather keep it in the code, rather than adding triggers to the database.
Here are my ideas:
I think I could do some crazy reflection automagic on the ObjectContext.SavingChanges event, but I don't think that is the best solution.
Or, I could add an interface that contains a DateUpdated
property, and implement it with all the classes that have that field. Then use ObjectContext.SavingChanges event to set the property on all of the changed objects that implement that interface.
Any ideas?
Thanks in advance!