c# - 在 DbContext 中访问 HttpContext.Current.User.Identity.Name

标签 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

好像不能注入(inject)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;

关于c# - 在 DbContext 中访问 HttpContext.Current.User.Identity.Name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085285/

相关文章:

c# - 将 httpWebRequest.Connection 设置为 "Closed"时出现 ArgumentException

c# - 如何使用 MVVM 更新 Datagrid?

c# - 代码优先 Entity Framework 无法更新多对多相关实体

c# - Entity Framework 5 代码首先无法使模型工作

.net - 创建我们自己的 UoW/Repository 与直接使用 DbContext 之间的区别

c# - 从 DbSet 获取 DbContext

c# - 如何在 ASP.NET MVC 应用程序中实现搜索框?

c# - 非根对象的 JSON 反序列化

entity-framework - MVC 3 EF 4.1 dbContext - 删除具有不可为空的外键关系的一对多数据对象

c# - 如何将信息附加到 DbContext 发出的每个 SqlCommand?