0

I am using ObjectContext.SavingChanges event to update CreatedDate/UpdatedDate columns based on the object state like this

partial void OnContextCreated()
{
   //Extension of the Command Timeout to avoid processing problems.
   CommandTimeout = 600; //Time in seconds

   this.SavingChanges += new EventHandler(Entities_SavingChanges);
}

protected void Entities_SavingChanges(object sender, EventArgs e)
{
   var addedObjects = this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added);
   foreach (var addedObject in addedObjects)
   {
      var propertyInfo = addedObject.Entity.GetType().GetProperty("CreatedDate");
      if (propertyInfo != null)
      {
         propertyInfo.SetValue(addedObject.Entity, DateTime.UtcNow, null);
      }
    }

    var modifiedObjects = this.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified);
    foreach (var modifiedObject in modifiedObjects)
    {
       var propertyInfo = modifiedObject.Entity.GetType().GetProperty("UpdatedDate");
       if (propertyInfo != null)
       {
          propertyInfo.SetValue(modifiedObject.Entity, DateTime.UtcNow, null);
       }
    }
}

I have two more columns CreatedUser and UpdatedUser.

Is there any way to update those using current user name from context?

Ofcource, System.HttpContext.Current is null here as this is my separate class library project, which I access through WCF service.

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • HttpContext.Current will not be null because the code is in a different dll. – qujck Feb 19 '13 at 08:12
  • No, HttpContext.Current is not null because the code is in a different DLL. HttpContext.Current is null because this code is running inside a WCF service. – Darin Dimitrov Feb 19 '13 at 08:15

1 Answers1

0

If this code resides in a WCF service that you consume from your ASP.NET MVC application you could send the current username as parameter to the method you are invoking.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks! I have updated my question to make it more clear. I am attaching event to ObjectContext so this is called whenever ObjectContext.SaveChanges method is called. So this is something generic thing I am doing for all the objects saving. – Pawan Nogariya Feb 19 '13 at 08:06
  • Oh and the code does not reside inside the web service, it is a separate library project. Web service uses this library project's reference – Pawan Nogariya Feb 19 '13 at 08:08
  • So you are calling a Web Service? The fact that this web service uses a class library project is irrelevant. The important thing is that you are calling a WCF webservice from your MVC application and want to *flow* the current user to it, right? – Darin Dimitrov Feb 19 '13 at 08:10
  • Well that's your problem. HttpContext.Current will be null in your WCF project. As I already suggested you in my answer you could pass the username as parameter to your method. – Darin Dimitrov Feb 19 '13 at 08:19
  • But which method? I am exposing number of methods those add or insert records to the database of different object types and whenever any entry is made to the database I want it to update either CreatedUser or UdpatedUser column with the current user before saving it. So to pass username from all the methods of service??? – Pawan Nogariya Feb 19 '13 at 08:23
  • 1
    Various options exists in this case. For example you could send the username of the currently authenticated user as a custom header: http://stackoverflow.com/questions/964433/how-to-add-a-custom-header-to-every-wcf-calls – Darin Dimitrov Feb 19 '13 at 08:29