通过 OnContextCreated() 访问实体部分类中的用户?

发布于 2025-01-08 06:27:23 字数 1472 浏览 0 评论 0原文

我的所有表都有通用的审核字段:modifiedBy、modifiedDateTime 等。

我希望自动设置这些字段,并且可以使用以下代码设置其中的大部分字段:

partial class myEntities
{
  partial void OnContextCreated()
  {
     this.SavingChanges += new EventHandler(Entities_SavingChanges);
  }

  private void Entities_SavingChanges(object sender, EventArgs e)
  {

     IEnumerable<ObjectStateEntry> objectStateEntries =
         from ose
         in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
         where ose.Entity != null
         select ose;

     var auditDate = DateTime.Now;
     var auditUser = System.Web.HttpContext.Current.User.Identity.Name;//I wish

     foreach (ObjectStateEntry entry in objectStateEntries)
     {
        ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues.DataRecordInfo.FieldMetadata;
        FieldMetadata modifiedField = fieldsMetaData.Where(f => f.FieldType.Name == "ModifiedBy").FirstOrDefault();
        if (modifiedField.FieldType != null)
        {
           string fieldTypeName = modifiedField.FieldType.TypeUsage.EdmType.Name;
           if (fieldTypeName == PrimitiveTypeKind.String.ToString())
           {
              entry.CurrentValues.SetString(modifiedField.Ordinal, auditUser);
           }
        }
     }

  }

}

问题是,似乎没有任何方法可以获取当前用户的访问权限。该应用程序仅适用于 Intranet,使用 Windows 身份验证。

有没有办法传递参数或访问 HttpContext (这似乎不是一个好主意,但我被困住了)?有没有办法用信息填充 EventArgs?

All of my tables have common audit fields: modifiedBy,modifiedDateTime, etc.

I would like to have these automatically set, and can set most of them with the following code:

partial class myEntities
{
  partial void OnContextCreated()
  {
     this.SavingChanges += new EventHandler(Entities_SavingChanges);
  }

  private void Entities_SavingChanges(object sender, EventArgs e)
  {

     IEnumerable<ObjectStateEntry> objectStateEntries =
         from ose
         in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
         where ose.Entity != null
         select ose;

     var auditDate = DateTime.Now;
     var auditUser = System.Web.HttpContext.Current.User.Identity.Name;//I wish

     foreach (ObjectStateEntry entry in objectStateEntries)
     {
        ReadOnlyCollection<FieldMetadata> fieldsMetaData = entry.CurrentValues.DataRecordInfo.FieldMetadata;
        FieldMetadata modifiedField = fieldsMetaData.Where(f => f.FieldType.Name == "ModifiedBy").FirstOrDefault();
        if (modifiedField.FieldType != null)
        {
           string fieldTypeName = modifiedField.FieldType.TypeUsage.EdmType.Name;
           if (fieldTypeName == PrimitiveTypeKind.String.ToString())
           {
              entry.CurrentValues.SetString(modifiedField.Ordinal, auditUser);
           }
        }
     }

  }

}

The problem is that there doesn't appear to be any way to get access to the current user. The app is intranet only, using Windows auth.

Is there a way to either pass in a parameter, or get access to the HttpContext (which doesn't seem like it would be a good idea, but I'm stuck)? Is there a way to populate the EventArgs with information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2025-01-15 06:27:23

查看发帖者重写了 SaveChanges 方法的部分(页面下方的第 6 个代码框)。这样您就可以传入 UserID 并执行审核,而不必使用事件处理程序。

Check out the section where the poster has overridden the SaveChanges method (6th code box down on the page). This way you can pass in the UserID and perform your audit and not have to use an event handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文