i'm implementing generic auditing in class. want detach entity context , serialize , save json string of serialized entity in db part of audit logic. because objects have references serializing fails reason. implemented logic set null class.propertytype.isgenerictype
, works fine. after creating audit log , call context.savechanges
, saves entity unset properties changes did detach context.
anyway can make code work?
note: newitem still being tracked context...
context.entry<t>( newitem ).state = system.data.entity.entitystate.detached; context.savechanges(); var props = newitem.gettype().getproperties().where( p => p.propertytype.isgenerictype ); foreach ( var item in props ) { item.setvalue( newitem, null ); } auditlog log = new auditlog() { createdon = datetime.now, modifiedon = datetime.now, type = ( int32 ) activity, actiontable = newitem.gettype().name, userid = ( ( currentuser != null ) ? currentuser.id : 0 ), afterimage = new javascriptserializer().serialize( newitem ), modifiedby = ( ( currentuser != null ) ? currentuser.email : "system" ), comments = string.format( "created new {0}", newitem.gettype().name ), beforeimage = ( olditem != null ) ? new javascriptserializer().serialize( olditem ) : string.empty, objectid = ( int32 ) newitem.gettype().getproperties().firstordefault( x => x.name == "id" ).getvalue( newitem ), parameters = new javascriptserializer().serialize( httpcontext.current.request.requestcontext.routedata.values ) }; context.logs.add( log ); context.savechanges(); // when execute this, changes in foreach loop saved newitem
thanks in advance.
Comments
Post a Comment