c# - Entity Framework/SQL2008 - How to Automatically Update LastModified fields for Entities? -


if have following entity:

public class pocowithdates {    public string poconame { get; set; }    public datetime createdon { get; set; }    public datetime lastmodified { get; set; } } 

which corresponds sql server 2008 table same name/attributes...

how can automatically:

  1. set createdon/lastmodified field record now (when doing insert)
  2. set lastmodified field record now (when doing update)

when automatically, mean want able this:

poco.name = "changing name"; repository.save();  

not this:

poco.name = "changing name"; poco.lastmodified = datetime.now; repository.save(); 

behind scenes, "something" should automatically update datetime fields. "something"?

i'm using entity framework 4.0 - there way ef can automatically me? (a special setting in edmx maybe?)

from sql server side, can use defaultvalue, work insert's (not update's).

similarly, can set default value using constructor on poco's, again work when instantiating object.

and of course could use triggers, it's not ideal.

because i'm using entity framework, can hook savingchanges event , update date fields here, problem need become "aware" of poco's (at moment, repository implemented generics). need sort of oo trickery (like make poco's implement interface, , call method on that). i'm not adversed that, if have that, rather manually set fields.

i'm looking sql server 2008 or entity framework 4.0 solution. (or smart .net way)

any ideas?

edit

thanks @marc_s answer, went solution better scenario.

i know i'm little late party, solved project i'm working on , thought i'd share solution.

first, make solution more re-usable, created base class timestamp properties:

public class entitybase {     public datetime? createddate { get; set; }     public datetime? lastmodifieddate { get; set; } } 

then overrode savechanges method on dbcontext:

public class mycontext : dbcontext {     public override int savechanges()     {         objectcontext context = ((iobjectcontextadapter)this).objectcontext;          //find entities added/modified inherit entitybase         ienumerable<objectstateentry> objectstateentries =             e in context.objectstatemanager.getobjectstateentries(entitystate.added | entitystate.modified)                             e.isrelationship == false &&                 e.entity != null &&                 typeof(entitybase).isassignablefrom(e.entity.gettype())             select e;          var currenttime = datetime.now;          foreach (var entry in objectstateentries)         {             var entitybase = entry.entity entitybase;              if (entry.state == entitystate.added)             {                 entitybase.createddate = currenttime;             }              entitybase.lastmodifieddate = currenttime;         }          return base.savechanges();     } } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -