访问DbContext中的HttpContext.Current.User.Identity.Name

时间:2015-06-27 04:53:17

标签: c# entity-framework dbcontext structuremap3

我将使用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配置项目中(我在另一个类库中使用了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。

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

查看此链接http://techbrij.com/service-layer-entity-framework-asp-net-mvc-unit-testing

作者的解决方案看起来像你的(但他使用的是AutoFac而不是StructureMap)。他的&#34;技巧&#34;获得&#34;名称&#34;是Thread.CurrentPrincipal.Identity.Name;

另一件事,恕我直言,我认为你应该在审计日期使用DateTimeOffSet而不是DateTime。使用DateTimeOffSet您不会遇到不同的时区问题。像这样:

DateTimeOffSet auditDate = DateTime.UtcNow;

答案 1 :(得分:0)