I have a table with DateCreated and DateUpdated columns and using Entity Framework to insert/update values to the database.
I need the DateCreated column to get the SQL Server’s GetDate() value on insertion only.
DateUpdated column value should always get updated with current GetDate() value on insertion and update both.
For the DateCreated column, I’ve set StoreGeneratedPattern="Computed" and on SQL Server table I’ve set default value of the column to be GetDate(), which works nicely as expected.
For the DateUpdated column I could not find a way to get the GetDate() value automatically set each time an entry is updated. This value get’s set only when an entry is inserted.
Could someone shed some light on this.
If you want
DateUpdatedto be set by the database, you can use a trigger to set the value togetdate(). I believe EF will also get the value set by the trigger if you setStoreGeneratedPattern="Computed"forDateUpdated.For reference, your trigger would look something like this (you’ll have to update per your table’s PK):