Using computed DateTime columns in the Entity Framework

StoreGeneratedPattern="Computed" works flawlessly on timestamp columns. But you cannot display TimeStamp as a human readable form.

Therefore, you cannot show the user "This string has been changed to {TimeStamp} " because you simply cannot parse the SQL Timestamp in a DateTime.

Now I can create calculated columns for DateTime columns, so I don’t need to set it manually? myEntity.LastModification = DateTime.Now

Not to mention that it would be quite error prone if some user really wanted to do something bad by simply changing the time of their computer.

+7
source share
1 answer

Yes, as described here: Is there a way to specify default values ​​for columns? Some DateTime columns in my database have getdate (), but EF does not know these defaults

Exposure:

... If you set StoreGeneratedPattern to Identity (if the value is calculated only upon insertion) or Computed (if it is calculated on each update), then when you call SaveChanges EF will receive the value generated in the database and return it to update your object ...

EDIT: in order to update the database server modification date, follow this answer https://stackoverflow.com/a/318618/ Computed will reboot

+1
source

All Articles