Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Timestamp - Automatically updating a timestamp column?

Tags:

c#

linq

I am not sure if there is a quick way to solve this problem.

We have LINQ objects/tables that have a Last Update column which needs to be updated anytime the object is changed.

We can come up with several "dirty" solutions for solving this, but not sure we are going down the right route.

Is there an easy way(possibly, in a LINQ Object partial,DataContext,etc) to be able to say,If you INSERT/UPDATE, always set "Last Update" column to DateTime.Now?

Update We have operations on this table in thousands of locations in our application, and therefor we are trying to find a way to let LINQ accomplish this for us, without having to find every location this object is updated.

like image 501
TheJediCowboy Avatar asked Dec 28 '22 16:12

TheJediCowboy


2 Answers

You could create a trigger which automatically updates this info anytime the table data is touched for insert/update.

like image 112
VoodooChild Avatar answered Jan 14 '23 12:01

VoodooChild


You can extend your DataContext and override the Update methods, see here:

http://msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic21

Make sure you call the base version of whatever methods you override after setting the Last Update time.

like image 27
BishopRook Avatar answered Jan 14 '23 14:01

BishopRook