I'm going to use the UserName for tracking Created and Modified fields. To do that I have referenced to the System.Web assembly directly inside DbContext:
public void auditFields()
{
var auditDate = DateTime.Now;
foreach (var entry in this.ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Detached:
break;
case EntityState.Unchanged:
break;
case EntityState.Added:
entry.Entity.CreatedOn = auditDate;
entry.Entity.ModifiedOn = auditDate;
entry.Entity.CreatedBy = HttpContext.Current.User.Identity.Name ?? "anonymouse";
entry.Entity.ModifiedBy = HttpContext.Current.User.Identity.Name ?? "anonymouse";
break;
case EntityState.Deleted:
break;
case EntityState.Modified:
entry.Entity.ModifiedOn = auditDate;
entry.Entity.ModifiedBy = HttpContext.Current.User.Identity.Name ?? "anonymouse";
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
It works, But it's tightly coupling the DbContext with HttpContext which is not a good idea in case we'll expose the DbContext to a non-web environment. So I use this way:
public class ApplicationDbContext :
IdentityDbContext<ApplicationUser, CustomRole, int, CustomUserLogin, CustomUserRole, CustomUserClaim>,
IUnitOfWork
{
public ApplicationDbContext()
: base("ConnectionString")
{
}
public ApplicationDbContext(string userName)
: base("ConnectionString")
{
UserName = userName;
}
//Other codes
public string UserName
{
get;
private set;
}
public void auditFields()
{
var auditDate = DateTime.Now;
foreach (var entry in this.ChangeTracker.Entries<BaseEntity>())
{
switch (entry.State)
{
case EntityState.Detached:
break;
case EntityState.Unchanged:
break;
case EntityState.Added:
entry.Entity.CreatedOn = auditDate;
entry.Entity.ModifiedOn = auditDate;
entry.Entity.CreatedBy = UserName ?? "anonymouse";
entry.Entity.ModifiedBy = UserName ?? "anonymouse";
break;
case EntityState.Deleted:
break;
case EntityState.Modified:
entry.Entity.ModifiedOn = auditDate;
entry.Entity.ModifiedBy = UserName ?? "anonymouse";
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
And in the Ioc config project (I'm using structureMap in another class library):
ioc.For<IUnitOfWork>()
.HybridHttpOrThreadLocalScoped()
.Use<ApplicationDbContext>()
.Ctor<string>().Is(HttpContext.Current.User.Identity.Name);
But when I run the application I'll get this error in above line:
Object reference not set to an instance of an object
It seems that it can't inject HttpContext.
Any idea?
Take a look at this link http://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing
The Author's solution looks like yours (but he used AutoFac instead of StructureMap). His "trick" to get the "Name" was Thread.CurrentPrincipal.Identity.Name;
Another thing, IMHO, I think you should use DateTimeOffSet
instead of DateTime
for the audit date. Using DateTimeOffSet
you won't have problems with differents time zone. Like this:
DateTimeOffSet auditDate = DateTime.UtcNow;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With