There is a need from a customer to log every data change to a logging table with the actual user who made the modification. The application is using one SQL user to access t
Simply force an execution of the SET CONTEXT_INFO by using your DbContext or ObjectContext:
...
FileMoverContext context = new FileMoverContext();
context.SetSessionContextInfo(Environment.UserName);
...
context.SaveChanges();
FileMoverContext inherits from DbContext and has a SetSessionContextInfo method. Here is what my SetSessionContextInfo(...) looks like:
public bool SetSessionContextInfo(string infoValue)
{
try
{
if (infoValue == null)
throw new ArgumentNullException("infoValue");
string rawQuery =
@"DECLARE @temp varbinary(128)
SET @temp = CONVERT(varbinary(128), '";
rawQuery = rawQuery + infoValue + @"');
SET CONTEXT_INFO @temp";
this.Database.ExecuteSqlCommand(rawQuery);
return true;
}
catch (Exception e)
{
return false;
}
}
Now you just set up a database trigger which can access the CONTEXT_INFO() and set a database field using it.