繁体   English   中英

实体框架从saveChanges中的contex获取用户

[英]Entity Framework get user from contex in saveChanges

我的解决方案中有两个项目,UI作为mvc,第一个用于实体模型代码的类项目。 我的模型中有多个实体,但是现在我需要通过新的审计字段来扩展它们,并在其中保存谁更改了实体。 我添加了新界面

public interface IAuditable
{
    /// <summary>Gets or sets the name.</summary>
    /// <value>The name.</value>
    DateTime CreatedDate { get; set; }

    /// <summary>Gets or sets the name.</summary>
    /// <value>The name.</value>
    string CreatedBy { get; set; }

    /// <summary>Gets or sets the name.</summary>
    /// <value>The name.</value>
    DateTime UpdatedDate { get; set; }

    /// <summary>Gets or sets the name.</summary>
    /// <value>The name.</value>
    string UpdatedBy { get; set; }
}

并尝试以这种方式扩展SaveChanges

foreach (var auditableEntity in ChangeTracker.Entries<IAuditable>())
                {
                    if (auditableEntity.State == EntityState.Added ||
                        auditableEntity.State == EntityState.Modified)
                    {
                        // implementation may change based on the useage scenario, this
                        // sample is for forma authentication.
                        string currentUser = ; 

                        // modify updated date and updated by column for 
                        // adds of updates.
                        auditableEntity.Entity.UpdatedDate = DateTime.Now;
                        auditableEntity.Entity.UpdatedBy = 

                        // pupulate created date and created by columns for
                        // newly added record.
                        if (auditableEntity.State == EntityState.Added)
                        {
                            auditableEntity.Entity.CreatedDate = DateTime.Now;
                            auditableEntity.Entity.CreatedBy = currentUser;
                        }
                        else
                        {
                            auditableEntity.Property(p => p.CreatedDate).IsModified = false;
                            auditableEntity.Property(p => p.CreatedBy).IsModified = false;
                        }
                    }

但是如何在这里获取用户名? 我不能使用任何httpContex getUser,因为这是类项目。 有任何想法吗?

这是我的重点

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDbContext

所以我想通过另一个字段LogedUserName扩展ApplicationUser,并在用户登录时填充它,但是如何在SaveChanges方法中获取此字段?

如果您确定此类类库将始终在ASP.NET管道中使用,则实际上可以访问HttpContext 您需要在System.Web中引用System.Web ,然后:

using System.Web;
[...]
public void SaveChanges()
{
    var username = HttpContext.Current.User.Identity.Name;
}

在这种情况下, HttpContext是静态类,而不是属性。

当然,如果在ASP.NET管道之外(例如,在WPF应用程序,控制台应用程序等中)使用此类,则这样做将严重失败。 同样,这样做似乎并不干净。 但这可能是最快的方法,需要最少的现有代码更改。

另一种方法是将用户名或整个身份传递给负责保存更改的类,或者直接传递给SaveChanges方法。

一种实现可能如下所示:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDbContext
{
    private IPrincipal _currentUser;
    public ApplicationDbContext(IPrincipal currentUser)
    {
        _currentUser = currentUser;
    }
}

然后在Controller中(如果直接在MVC控制器中使用上下文):

using(var db = new ApplicationDbContext(User))
{
    [...]
}

其中, User是拥有当前用户的控制器属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM