DateCreated or Modified Column - Entity Framework or using triggers on SQL Server -


after read 1 question in attached link, got sense of how set datecreated , datemodified columns in entity framework , use in application. in old sql way though, trigger way more popular because more secure dba point of view.

so advice on way best practice? should set in entity framework purpose of application integrity? or should use trigger make more sense data security point of view? or there way compose trigger in entity framework? thanks.

ef codefirst: rails-style created , modified columns

btw, though doesn't matter much, building app using asp.net mvc c#.

opinion: triggers hidden behaviour, unless go looking them won't realise there. keep db 'dumb' possible when using ef, since i'm using ef team wont need maintain sql code.

for solution (mix of asp.net webforms , mvc in c# business logic in project contains datacontext):

i had similar issue, , although situation more complex (databasefirst, required custom tt file), solution same.

i created interface:

public interface itrackableentity {     datetime createddatetime { get; set; }     int createduserid { get; set; }     datetime modifieddatetime { get; set; }     int modifieduserid { get; set; } } 

then implemented interface on entities needed (because solution databasefirst, updated tt file check if table had 4 columns, , if added interface output).

update: here's changes tt file, updated entityclassopening() method:

public string entityclassopening(entitytype entity) {     var trackableentitypropnames = new string[] { "createduserid", "createddatetime", "modifieduserid", "modifieddatetime" };     var propnames = entity.properties.select(p => p.name);     var istrackable = trackableentitypropnames.all(s => propnames.contains(s));     var inherits = new list<string>();     if (!string.isnullorempty(_typemapper.gettypename(entity.basetype)))     {         inherits.add(_typemapper.gettypename(entity.basetype));     }     if (istrackable)     {         inherits.add("itrackableentity");     }      return string.format(         cultureinfo.invariantculture,         "{0} {1}partial class {2}{3}",         accessibility.fortype(entity),         _code.spaceafter(_code.abstractoption(entity)),         _code.escape(entity),         _code.stringbefore(" : ", string.join(", ", inherits))); } 

the thing left add following partial datacontext class:

    public override int savechanges()     {         // fix trackable entities         var trackables = changetracker.entries<itrackableentity>();          if (trackables != null)         {             // added             foreach (var item in trackables.where(t => t.state == entitystate.added))             {                 item.entity.createddatetime = system.datetime.now;                 item.entity.createduserid = _userid;                 item.entity.modifieddatetime = system.datetime.now;                 item.entity.modifieduserid = _userid;             }             // modified             foreach (var item in trackables.where(t => t.state == entitystate.modified))             {                 item.entity.modifieddatetime = system.datetime.now;                 item.entity.modifieduserid = _userid;             }         }          return base.savechanges();     } 

note saved current user id in private field on datacontext class each time created it.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -