覆盖 SaveChanges 并设置 ModifiedDate,但如何设置 ModifiedBy? [英] Overriding SaveChanges and setting ModifiedDate, but how do I set ModifiedBy?

查看:22
本文介绍了覆盖 SaveChanges 并设置 ModifiedDate,但如何设置 ModifiedBy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 UI、业务(实体)和数据 (DbContext) 层的 ASP.NET MVC3 Web 应用程序.我使用 Entity Framework 4.1 Code First.现在,我正在覆盖数据层中的 DbContext.SaveChanges(),以便我可以为对实现我的 ModifiedDate>IAuditable 界面.我有一个返回 DateTime.Now 的静态 DateProvider 类和方法 (GetCurrentDate)(除非我正在运行测试,在这种情况下,它返回我告诉它的任何内容).

I have an ASP.NET MVC3 web application with UI, Business (entities), and Data (DbContext) layers. I am using Entity Framework 4.1 Code First. Right now, I am overriding the DbContext.SaveChanges() in the Data layer so that I can set the ModifiedDate for all changes made to any entity objects that implement my IAuditable interface. I have a static DateProvider class and method (GetCurrentDate) that returns DateTime.Now (unless I'm running a test, in which case, it returns whatever I told it to).

我还想为当前用户自动设置 ModifiedBy 属性.这样做的最佳方法是什么?框架中是否有内置的东西可以让我访问这些信息,或者我是否需要设置一些类似于 DateProvider 类的东西?这是一个 Active Directory 环境,我们在 IIS 中使用 WindowsAuthentication.

I would like to automatically set the ModifiedBy property to the current user as well. What is the best way to go about doing this? Is there something that is built in the framework that will allow me to access this information or do I need to set something up kind of like the DateProvider class? This is an Active Directory environment and we use WindowsAuthentication in IIS.

这是我的 SaveChanges 代码:

Here is my SaveChanges code:

public override int SaveChanges()
{
    var changeSet = ChangeTracker.Entries<IAuditable>();

    if (changeSet != null)
    {
        foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))
        {
            entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();
        }
    }
    return base.SaveChanges();
}

推荐答案

您可以使用 HttpContext.Current.User.Identity.Name 获取当前用户的名称.

You can use the HttpContext.Current.User.Identity.Name to get the name of the current user.

public override int SaveChanges()
{
    var changeSet = ChangeTracker.Entries<IAuditable>();

    if (changeSet != null)
    {
        foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))
        {
            entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();
            entry.Entity.ModifiedBy = HttpContext.Current.User.Identity.Name;
        }
    }
    return base.SaveChanges();
}

更好的方法是使用构造函数注入将当前用户传递给上下文

Better way to do this would be to use constructor injection to pass the current user to the context

public class MyContext : DbContext
{
    public MyContext(string userName)
    {
        UserName = userName;
    }

    public string UserName
    {
        get; private set;
    }

    public override int SaveChanges()
    {
       var changeSet = ChangeTracker.Entries<IAuditable>();

       if (changeSet != null)
       {
          foreach (var entry in changeSet.Where(c => c.State != EntityState.Unchanged))
          {
              entry.Entity.ModifiedDate = DateProvider.GetCurrentDate();
              entry.Entity.ModifiedBy = UserName;
          }
       }
       return base.SaveChanges();
   }
}

这篇关于覆盖 SaveChanges 并设置 ModifiedDate,但如何设置 ModifiedBy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆