1

I would like to have a field which is updated on every change(insertion, modification), basically, it's a "ModificationDate".

I saw there is a "Computed" StoreGeneratedPattern in EF. But since this will not set the date but only take the value generated by the database, I need to know what should I do on the database to have this default value set at the equivalent of GetDate() on every modification/insertion?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • 1
    Almost duplicate with: http://stackoverflow.com/questions/7737945/how-to-create-trigger-for-auto-update-modified-date-with-sql-server-2008 – David Brabant Jun 12 '12 at 07:01

1 Answers1

0

You need to create either INSTEAD OF update trigger where you get data and perform update yourselves or AFTER UPDATE trigger where you execute one more update to set the date.

It would be easier to override SaveChanges and set the date in your application prior to saving to database.

Josien
  • 13,079
  • 5
  • 36
  • 53
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Seriously? There is a "Computed Column Specification", with a Formula and a IsPersisted in the table designer. I saw this Computed StoreGeneratedPattern as correct answer in a MCTS exam, so I think it's the better way – J4N Jun 12 '12 at 07:06
  • The `StoreGeneratedPattern.Computed` only tells EF that database is generated in the database so it doesn't send a value and queries the value after update but you must do the generation in the database. – Ladislav Mrnka Jun 12 '12 at 07:13
  • Yeah I know, but what is this "Computed column specification" if it cannot be used to indicate how to generate this column? – J4N Jun 12 '12 at 09:54
  • But the purpose of computed column in table design is to compute some value based on other columns. – Ladislav Mrnka Jun 12 '12 at 11:49