为什么我收到IdentityRole错误?

时间:2014-04-01 21:01:35

标签: asp.net-mvc entity-framework entity-framework-5 asp.net-identity asp.net-mvc-5.1

使用Identity 2.0注册用户后,在数据库中创建用户,Account控制器中有代码用于创建身份并在用户签名。这是我收到错误的时候。以下是产生错误的代码:

var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

这是错误:

  

实体类型IdentityRole不是当前模型的一部分   上下文。

我的模型看起来像这样:

namespace MyApp.Models
{
    public class ApplicationUser : IdentityUser
    {
        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }

        [Required]
        [StringLength(50)]
        public string LastName { get; set; }
    }

    public class ApplicationRole : IdentityRole
    {
        [Required]
        [StringLength(50)]
        public string ProperName { get; set; }

        [Required]
        public string Description { get; set; }
    }


    public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
    {
        public MyAppDb()
            : base("MyAppDb")
        {
        }
    }
}

我的UserManager在帐户控制器中创建如下:

namespace MyApp.Controllers
{
    [Authorize]
    public class AccountController : BaseController
    {
        private UserManager<ApplicationUser> _userManager;

        public AccountController()
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }

        public UserManager<ApplicationUser> UserManager
        {
            get
            {
                return _userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyAppDb()));
            }
            private set
            {
                _userManager = value;
            }
        }

        ...
    }
}

1 个答案:

答案 0 :(得分:5)

我花了很多时间在这个问题上并发布了许多问题以便深入了解它。我当然希望这些信息有助于其他人。

基本上,自定义User很简单,有几个例子可供使用。但是,自定义Role是有问题的,我发现的唯一示例远远超出了“开箱即用”的范围,我在解决Id问题后得到了一个工作。在这种情况下的问题是我必须自己创建Id。以下是该问题:UserManager.Create(user, password) thowing EntityValidationError saying Id is required?

因此,在简单的示例中,请注意,当您只是自定义User DbContext继承自IdentityDbContext时,您只需提供IdentityUser,在我的情况下,它被调用ApplicationUser,看起来像IdentityDbContext<ApplicationUser>。另请注意,UserManagerUserStore也是IdentityUser提供的,在大多数示例中都是new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()))

但是,一旦您自定义了Role ,就需要传递所有内容,包括TKey字段的密钥类型Id。我看起来像IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>,因为我只定制了UserRole

但是你还没有完成,因为常规UserManager不再理解了。 我认为这是身份伙伴可以/应该修复的东西,但我离题了。所以我必须实现我自己的UserStoreUserManager并注意(这是部分这让我感到高兴)你必须在这里提供KType ,或者实例化你的自定义UserManager会给你一个编译时错误:

public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public ApplicationUserStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
    public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
        : base(store)
    {
    }
}

并在帐户控制器中实例化UserManager,如new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()))

public class AccountController : BaseController
{
    private ApplicationUserManager _userManager;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager = new ApplicationUserManager(new ApplicationUserStore(new MyAppDb()));
        }
        private set
        {
            _userManager = value;
        }
    }

    ...

}

希望这有帮助!

相关问题