Sir*_*ifi 5 c# entity-framework dbcontext structuremap3
我将使用UserName来跟踪“创建”和“修改”字段。为此,我直接在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
它可以正常工作,但是将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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Ioc配置项目中(我在另一个类库中使用structureMap):
ioc.For<IUnitOfWork>()
.HybridHttpOrThreadLocalScoped()
.Use<ApplicationDbContext>()
.Ctor<string>().Is(HttpContext.Current.User.Identity.Name);
Run Code Online (Sandbox Code Playgroud)
但是,当我运行应用程序时,我会在上一行得到此错误:
Object reference not set to an instance of an object
Run Code Online (Sandbox Code Playgroud)
似乎无法注入HttpContext。
任何的想法?
看看这个链接http://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing
作者的解决方案看起来像你的(但他使用 AutoFac 而不是 StructureMap)。他获得“名字”的“伎俩”是Thread.CurrentPrincipal.Identity.Name;
另一件事,恕我直言,我认为你应该使用DateTimeOffSet而不是DateTime审计日期。使用DateTimeOffSet你不会有不同时区的问题。像这样:
DateTimeOffSet auditDate = DateTime.UtcNow;
Run Code Online (Sandbox Code Playgroud)