Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. public partial class DataContext : DbContext, IDataContext
  2. {
  3. public DataContext()
  4. : base("name=DataContext")
  5. {
  6. }
  7.  
  8. public DataContext(DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection)
  9. {
  10.  
  11. }
  12.  
  13. public string UserID { get; set; }
  14.  
  15. public override int SaveChanges()
  16. {
  17. SetDbSession();
  18. return base.SaveChanges();
  19. }
  20.  
  21. private void SetDbSession()
  22. {
  23. //Open a connection to the database so the session is set up
  24. this.Database.Connection.Open();
  25. //Set the db session
  26. //ExecuteSqlCommand ot to be used as it will close the connection
  27. using (var cmd = this.Database.Connection.CreateCommand())
  28. {
  29. var parm = cmd.CreateParameter();
  30. parm.ParameterName = "@userId";
  31. parm.Value = UserID ;
  32. cmd.CommandText = "SetDbSession";
  33. cmd.CommandType = System.Data.CommandType.StoredProcedure;
  34. cmd.Parameters.Add(parm);
  35. cmd.ExecuteNonQuery();
  36. };
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement