I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext) layers. I am using Entity Framework 4.1 Code First. Right now, I am overriding the DbContext.SaveChanges()
in the Data layer so that I can set the ModifiedDate
for all changes made to any entity objects that implement my IAuditable
interface. I have a static DateProvider
class and method (GetCurrentDate) that returns DateTime.Now
(unless I'm running a test, in which case, it returns whatever I told it to).
I would like to automatically set the ModifiedBy
property to the current user as well. What is the best way to go about doing this? Is there something that is built in the framework that will allow me to access this information or do I need to set something up kind of like the DateProvider
class? This is an Active Directory environment and we use WindowsAuthentication
in IIS.
Here is my SaveChanges code:
public override int SaveChanges()
{
var changeSet = ChangeTracker.Entries<IAuditable>();
if (changeSet != null)
{
foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))
{
entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();
}
}
return base.SaveChanges();
}
You can use the
HttpContext.Current.User.Identity.Name
to get the name of the current user.Better way to do this would be to use constructor injection to pass the current user to the context
I also wanted to automate the population of audit fields on my MVC 4 / Entity Framework 5 application. I used information from @Eranga's answer and this blog: http://lourenco.co.za/blog/2013/07/audit-trails-concurrency-and-soft-deletion-with-entity-framework/ to make this approach work for me with Ninject - posting in case valuable to anyone else:
Created an interface and abstract class:
Used them in my entities:
Added a constructor to MyDbContext which accepted the HttpContext and overrode SaveChanges:
Finally - need to have a DbContext per request and pass HttpContext as below based on this answer: https://stackoverflow.com/a/3617961/1803682 - note that as
MyDbContext
is now Request scope, the Repositories must be as well.