Are stored procedures the only way to reliably set a datetime field with the server's modification time?
Here are the requirements:
- We have a table with a
lastModifiedOn
column. - We want that column to really, reliably, reflect the (utc) time at which the field was last updated in the database.
- We want that to still be the case when modifying the contents of the tables with Entity Framework.
Right now, we are doing that with a Modifi开发者_如何学PythoncationFunctionMapping/UpdateFunction
element in the MSL and a stored procedure defined in the SSDL file.
Is there another way of achieving the same effect, without having to use a Stored Procedure?
EDIT: In the end, I was not clear enough.
When I said that the "stored procedure [was] defined in the SSDL file", I meant to say that the stored procedure's name was defined in the SSDL file - but the problem was that I had to deploy stored procedures with these names on the database, which could conceivably create problems in the event of multiple EF models.
Now that I have understood the use of the <CommandText>
element in the SSDL <Function>
element, I have been able to put the custom SQL along with the rest of the Entity Framework model - which solved my problem.
You can override the lastModifiedOn
column with a trigger inserting current_timestamp
. I'm presuming the database server is a Sql Server.
I agree with Filip, in our application we also have a Column for LastModifiedOn and we have created a trigger on the tables which updates this column with Current timestamp whenever row is updated in the table.
The Thing is you would require to create two triggers on the table, 1. Update Trigger : that would update LastModifiedOn Column with current timestamp whenever a row is updated. 2. Insert Trigger : that would insert current timestamp in LastModifiedOn Column whenever a row is created in the table.
精彩评论