code_junkie

Logging every data change with Entity Framework

Nov 12th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. testEntities te = (testEntities)sender;
  2. DbConnection dc = te.Connection;
  3. DbCommand dcc = dc.CreateCommand();
  4. dcc.CommandType = CommandType.StoredProcedure;
  5. DbParameter dp = new EntityParameter();
  6. dp.ParameterName = "userid";
  7. dp.Value = textBox1.Text;
  8. dcc.CommandText = "userinit";
  9. dcc.Parameters.Add(dp);
  10. dcc.ExecuteNonQuery();
  11.  
  12. StringBuilder cStr = new StringBuilder("declare @tx char(50); set @tx='");
  13. cStr.Append(textBox1.Text);
  14. cStr.Append("'; declare @m binary(128); set @m = cast(@tx as binary(128)); set context_info @m;");
  15.  
  16. testEntities te = (testEntities)sender;
  17. DbConnection dc = te.Connection;
  18. DbCommand dcc = dc.CreateCommand();
  19. dcc.CommandType = CommandType.Text;
  20. dcc.CommandText = cStr.ToString();
  21. dcc.ExecuteNonQuery();
  22.  
  23. -- This is for the data
  24. create table datastuff (
  25. id int not null identity(1, 1),
  26. userid nvarchar(64) not null default(''),
  27. primary key(id)
  28. )
  29. go
  30.  
  31. -- This is for the log
  32. create table naplo (
  33. id int not null identity(1, 1),
  34. userid nvarchar(64) not null default(''),
  35. datum datetime not null default('2099-12-31'),
  36. primary key(id)
  37. )
  38. go
  39.  
  40. create trigger myTrigger on datastuff for insert as
  41.  
  42. declare @User_id int,
  43. @User_context varbinary(128),
  44. @User_id_temp varchar(64)
  45.  
  46. select @User_context = context_info
  47. from master.dbo.sysprocesses
  48. where spid=@@spid
  49.  
  50. set @User_id_temp = cast(@User_context as varchar(64))
  51.  
  52. declare @insuserid nvarchar(64)
  53.  
  54. select @insuserid=userid from inserted
  55.  
  56. insert into naplo(userid, datum)
  57. values(@User_id_temp, getdate())
  58.  
  59. go
  60.  
  61. create procedure userinit(@userid varchar(64))
  62. as
  63. begin
  64. declare @m binary(128)
  65. set @m = cast(@userid as binary(128))
  66. set context_info @m
  67. end
  68. go
  69.  
  70. using (testEntities te = new testEntities())
  71. {
  72. // Hook to the event
  73. te.SavingChanges += new EventHandler(te_SavingChanges);
  74.  
  75. // This is important, because the context info is set inside a connection
  76. te.Connection.Open();
  77.  
  78. // Add a new datastuff
  79. datastuff ds = new datastuff();
  80.  
  81. // This is coming from a text box of my test form
  82. ds.userid = textBox1.Text;
  83. te.AddTodatastuff(ds);
  84.  
  85. // Save the changes
  86. te.SaveChanges(true);
  87.  
  88. // This is not needed, only to make sure
  89. te.Connection.Close();
  90. }
  91.  
  92. // Take my entity
  93. testEntities te = (testEntities)sender;
  94.  
  95. // Get it's connection
  96. EntityConnection dc = (EntityConnection )te.Connection;
  97.  
  98. // This is important!
  99. DbConnection storeConnection = dc.StoreConnection;
  100.  
  101. // Create our command, which will call the userinit SP
  102. DbCommand command = storeConnection.CreateCommand();
  103. command.CommandText = "userinit";
  104. command.CommandType = CommandType.StoredProcedure;
  105.  
  106. // Put the user id as the parameter
  107. command.Parameters.Add(new SqlParameter("userid", textBox1.Text));
  108.  
  109. // Execute the command
  110. command.ExecuteNonQuery();
  111.  
  112. private int _contextUserId;
  113.  
  114. public void SomeMethod()
  115. {
  116. var db = new MyEntities();
  117. db.Connection.StateChange += this.Connection_StateChange;
  118. this._contextUserId = theCurrentUserId;
  119.  
  120. // whatever else you want to do
  121. }
  122.  
  123. private void Connection_StateChange(object sender, StateChangeEventArgs e)
  124. {
  125. // only do this when we first open the connection
  126. if (e.OriginalState == ConnectionState.Open ||
  127. e.CurrentState != ConnectionState.Open)
  128. return;
  129.  
  130. // use the existing open connection to set the context info
  131. var connection = ((EntityConnection) sender).StoreConnection;
  132. var command = connection.CreateCommand();
  133. command.CommandText = "proc_ContextInfoSet";
  134. command.CommandType = CommandType.StoredProcedure;
  135. command.Parameters.Add(new SqlParameter("ContextUserID", this._contextUserId));
  136. command.ExecuteNonQuery();
  137. }
Add Comment
Please, Sign In to add comment