1

Is there like some kind of base method I can tap into would that add DateTime.Now() when ever an EF entity is being created or updated?

I'm just wondering if there is a clever way to approach this without having to change every CRUD statement throughout the application.

Thanks.

Smith
  • 2,904
  • 3
  • 19
  • 25
  • 1
    Although there is no accepted answer here, [this post](http://stackoverflow.com/questions/3879011/entity-framework-sql2008-how-to-automatically-update-lastmodified-fields-for-e) has all the info you need. Override `SaveChanges` on your context and use `ObjectStateManager.GetObjectStateEntries()` to get the objects that have been modified. – jeffesp Jun 03 '13 at 16:15
  • Excellent. I'll look into that. Anything is better than having to manually add code into all my update and create statements. – Smith Jun 03 '13 at 16:34

1 Answers1

1

I've written up a blog post for any future visitors to this question. I use a base entity to add the columns to all entities and then override SaveChanges() to injects the times.

http://benjii.me/2014/03/track-created-and-modified-fields-automatically-with-entity-framework-code-first/

Ben Cull
  • 9,434
  • 7
  • 43
  • 38
  • I will show all the 4 columns createby/on updatedBy/on on the views that it not good. because while updating a record person can change createdBy/on. and if you use `[Scafold(false)]` as attribute on these 4 properties then when you edit earlier created model it will insert the null at the place of CreatedOn field.. – Deepak Sharma Feb 23 '15 at 12:30