Logging every data change with Entity Framework -
there need customer log every data change logging table actual user made modification. application using 1 sql user access database, need log "real" user id.
we can in t-sql writing triggers every table insert , update, , using context_info store user id. passed user id stored procedure, stored user id in contextinfo, , trigger use info write log rows log table.
i can not find place or way or how can similar using ef. main goal is: if make change in data via ef, log exact data change table in semi-automatic way (so don't want check every field change before saving object). using entitysql.
unfortunately have stick on sql 2000 data change capture introduced in sql2008 not option (but maybe that's not right way us).
any ideas, links or starting points?
[edit] notes: using objectcontext.savingchanges eventhandler, can point can inject sql statement initialize contextinfo. cannot mix ef , standard sql. can entityconnection cannot execute t-sql statement using it. or can connection string of entityconnection , create sqlconnection based on it, different connection, contextinfo not affect save made ef.
i tried following in savingchanges handler:
testentities te = (testentities)sender; dbconnection dc = te.connection; dbcommand dcc = dc.createcommand(); dcc.commandtype = commandtype.storedprocedure; dbparameter dp = new entityparameter(); dp.parametername = "userid"; dp.value = textbox1.text; dcc.commandtext = "userinit"; dcc.parameters.add(dp); dcc.executenonquery();
error: value of entitycommand.commandtext not valid storedprocedure command. same sqlparameter instead of entityparameter: sqlparameter cannot used.
stringbuilder cstr = new stringbuilder("declare @tx char(50); set @tx='"); cstr.append(textbox1.text); cstr.append("'; declare @m binary(128); set @m = cast(@tx binary(128)); set context_info @m;"); testentities te = (testentities)sender; dbconnection dc = te.connection; dbcommand dcc = dc.createcommand(); dcc.commandtype = commandtype.text; dcc.commandtext = cstr.tostring(); dcc.executenonquery();
error: query syntax not valid.
so here am, stuck create bridge between entity framework , ado.net. if can working, post proof of concept.
how handling context.savingchanges?
Comments
Post a Comment