【问题标题】:Identity 2.0 User with partial DbContext MVC 5具有部分 DbContext MVC 5 的 Identity 2.0 用户
【发布时间】:2016-02-21 18:05:57
【问题描述】:

我想在我的上下文部分类中添加一些“自动化”功能,例如CreatedByModifiedBy

我有 2 个项目: DataAccess 和 MVC(启动项目)。

我无法在文件夹 Context 中的上下文部分类中获取程序集引用(请参见照片),有什么方法可以做到这一点?我已经安装了所有必要的 nu-get 组件

所以,当我尝试拨打User.Identity.GetUserId(); 时,我有红色下划线 正如您在图片中看到的,我什至在该项目 (DataAccess) 中提取了类 IdentityModels.cs,但没有运气。

我的上下文类看起来像:

 using System;
 using System.Collections.Generic;
 using System.Data.Entity;
 using System.Data.Entity.Core.Objects;
 using System.Data.Entity.Infrastructure;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Net;
 using System.Text;
 using System.Threading.Tasks;
 using System.Web; 
 using System.Web.Mvc;
 using Microsoft.AspNet.Identity;
 using Microsoft.VisualBasic.ApplicationServices;
 using Owin;

 namespace DataAccess
{
public partial class SalesManagementEntities
{

    private bool doSaveChangesOverride = true;

    public bool DoSaveChangesOverride
    {
        set { doSaveChangesOverride = value; }
        get { return doSaveChangesOverride; }
    }

    public override int SaveChanges()
    {
        try
        {
            if (DoSaveChangesOverride)
                SaveChangesOverride();
            return base.SaveChanges();
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }

    public override Task<int> SaveChangesAsync()
    {
        if (DoSaveChangesOverride)
            SaveChangesOverride();

        return base.SaveChangesAsync();
    }

    private void SaveChangesOverride()
    {
        ObjectContext context = ((IObjectContextAdapter)this).ObjectContext;

        //Find all Entities that are Added/Modified that inherit from my EntityBase
        IEnumerable<ObjectStateEntry> objectStateEntries =
            from e in context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified)
            where
                e.IsRelationship == false &&
                e.Entity is IEntityBase
            select e;

        var currentTime = DateTime.Now;

        string userId = User.Identity.GetUserId();

        foreach (var entry in objectStateEntries)
        {
            var entityBase = entry.Entity as IEntityBase;

            if (entry.State == EntityState.Added)
            {
                entityBase.SetCreatedDateTime(currentTime);
                entityBase.SetCreatedBy(userId);

            }

            entityBase.SetModifiedDateTime(currentTime);
            entityBase.SetModifiedBy(userId);
        }
    }

}
}

【问题讨论】:

  • 试试 HttpContext.Current.User.Identity.GetUserId() 能行吗?

标签: asp.net-mvc entity-framework asp.net-identity-2


【解决方案1】:

以下代码:

User.Identity.GetUserId();

仅当您在 mvc 控制器中时才有效,因为 mvc 控制器公开了一个名为 User 的属性,该属性属于 IPrincipal 类型,可让您访问 IPrincipal 成员,如 Identity 等...

您的DbContext 没有User 属性,而是User 类,编译器尝试检查该类中是否存在名为Identity 的静态属性,并且找不到该属性并最终引发这是一个错误。

要在 DAL 层中获取当前用户 ID,您必须使用以下代码:

// There is no nuget package to install to get the user name.
// If the user name is unique into your database table then 
// you can request your DbContext and get the user id.
var userName = System.Web.HttpContext.Current.User.Identity.Name;

如果您想通过使用GetUserIdGetUserId&lt;T&gt; 扩展方法将用户ID 输入控制器,请使用以下代码:

// You need to install the Microsoft.AspNet.Identity.Core nuget package
// and get the user id like this:
var userId = System.Web.HttpContext.Current.User.Identity.GetUserId();

我建议您使用可以为您提供当前IIdentity 的服务。这样做将有助于单元测试,因为模拟当前用户会很容易。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2014-12-31
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多