在 DbContext 中访问 HttpContext.Current.User.Identity.Name
Accessing HttpContext.Current.User.Identity.Name in the DbContext
我将使用 UserName 来跟踪 Created 和 Modified 字段。为此,我直接在 DbContext 中引用了 System.Web 程序集:
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();
}
}
}
它可以工作,但它会将 DbContext 与 HttpContext 紧密耦合,这不是一个好主意,以防我们将 DbContext 暴露给非 Web 环境。
所以我这样使用:
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();
}
}
}
}
并且在 Ioc 配置项目中(我在另一个 class 库中使用 structureMap):
ioc.For<IUnitOfWork>()
.HybridHttpOrThreadLocalScoped()
.Use<ApplicationDbContext>()
.Ctor<string>().Is(HttpContext.Current.User.Identity.Name);
但是当我 运行 应用程序时,我会在上面的行中得到这个错误:
Object reference not set to an instance of an object
好像不能注入HttpContext
有什么想法吗?
看看这个linkhttp://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing
作者的解决方案看起来像你的(但他使用 AutoFac 而不是 StructureMap)。他获得 "Name" 的 "trick" 是 Thread.CurrentPrincipal.Identity.Name;
另一件事,恕我直言,我认为您应该使用 DateTimeOffSet
而不是 DateTime
作为审计日期。使用 DateTimeOffSet
您不会遇到不同时区的问题。像这样:
DateTimeOffSet auditDate = DateTime.UtcNow;
以下应该有所帮助
http://mikehadlow.blogspot.com/2008/08/taking-httpcontext-out-of-mvc-framework.html
我将使用 UserName 来跟踪 Created 和 Modified 字段。为此,我直接在 DbContext 中引用了 System.Web 程序集:
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();
}
}
}
它可以工作,但它会将 DbContext 与 HttpContext 紧密耦合,这不是一个好主意,以防我们将 DbContext 暴露给非 Web 环境。 所以我这样使用:
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();
}
}
}
}
并且在 Ioc 配置项目中(我在另一个 class 库中使用 structureMap):
ioc.For<IUnitOfWork>()
.HybridHttpOrThreadLocalScoped()
.Use<ApplicationDbContext>()
.Ctor<string>().Is(HttpContext.Current.User.Identity.Name);
但是当我 运行 应用程序时,我会在上面的行中得到这个错误:
Object reference not set to an instance of an object
好像不能注入HttpContext
有什么想法吗?
看看这个linkhttp://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing
作者的解决方案看起来像你的(但他使用 AutoFac 而不是 StructureMap)。他获得 "Name" 的 "trick" 是 Thread.CurrentPrincipal.Identity.Name;
另一件事,恕我直言,我认为您应该使用 DateTimeOffSet
而不是 DateTime
作为审计日期。使用 DateTimeOffSet
您不会遇到不同时区的问题。像这样:
DateTimeOffSet auditDate = DateTime.UtcNow;
以下应该有所帮助
http://mikehadlow.blogspot.com/2008/08/taking-httpcontext-out-of-mvc-framework.html