C# 实体框架在saveChanges中从contex获取用户

C# 实体框架在saveChanges中从contex获取用户,c#,entity-framework,entity-framework-4,entity-framework-5,entity-framework-6,C#,Entity Framework,Entity Framework 4,Entity Framework 5,Entity Framework 6,我的解决方案中有两个项目,作为mvc的UI和首先用于实体模型代码的类项目。我的模型中有几个实体,但现在我需要通过新的审计字段来扩展它们,在这里我需要保存谁更改了实体。 我添加了新的界面 public interface IAuditable { /// <summary>Gets or sets the name.</summary> /// <value>The name.</value> DateTime Created

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

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; }
}
公共接口IAuditable
{
///获取或设置名称。
///名字。
DateTime CreatedDate{get;set;}
///获取或设置名称。
///名字。
字符串CreatedBy{get;set;}
///获取或设置名称。
///名字。
DateTime UpdateDate{get;set;}
///获取或设置名称。
///名字。
由{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;
                        }
                    }
foreach(ChangeTracker.Entries()中的var auditableEntity)
{
如果(auditableEntity.State==EntityState.Add||
auditableEntity.State==EntityState.Modified)
{
//实现可能会根据使用场景而改变,这
//样品用于形式认证。
字符串currentUser=;
//修改的更新日期和按列更新
//添加一系列更新。
auditableEntity.Entity.UpdateDate=DateTime.Now;
auditableEntity.Entity.UpdatedBy=
//p计算的创建日期和按列创建
//新增加的记录。
if(auditableEntity.State==EntityState.Added)
{
auditableEntity.Entity.CreatedDate=DateTime.Now;
auditableEntity.Entity.CreatedBy=currentUser;
}
其他的
{
属性(p=>p.CreatedDate).IsModified=false;
属性(p=>p.CreatedBy).IsModified=false;
}
}
但是我如何在这里获得用户名?我不能使用任何httpContex getUser,因为这是类项目。有什么想法吗

这是我的上下文

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDbContext
public类ApplicationDbContext:IdentityDbContext,IDbContext

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

如果您确定此类库将始终在ASP.NET管道中使用,您实际上可以访问
HttpContext
。 您需要在类库中引用
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;
    }
}

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

我想负责协调的控制器需要提供该属性。请在此处查找.net核心解决方案:
using(var db = new ApplicationDbContext(User))
{
    [...]
}