【问题标题】:Tell UserManager to use AspNetRoles告诉 UserManager 使用 AspNetRoles
【发布时间】:2014-04-18 00:47:54
【问题描述】:

我正在尝试将 MVC3 应用程序迁移到 MVC5。我用过这些文章:

旧应用程序使用数据库优先模型(使用 EDMX 文件建模)。

当我尝试更新用户角色时,出现以下错误:

附加信息:实体类型 IdentityRole 不是当前上下文模型的一部分。

在这个方法中(um.AddToRole):

public bool AddUserToRole(string userId, string roleName)
    {
        var um = new UserManager<AspNetUsers>(
            new UserStore<AspNetUsers>(new Entities()));
        var idResult = um.AddToRole(userId, roleName);
        return idResult.Succeeded;
    }

如何告诉 UserManager 使用 AspNetRoles 类?

最好的问候, 马尔科

AspNet 用户

namespace NursingHome.Models
{
    using System;
    using System.Collections.Generic;

    public partial class AspNetUsers
    {
        public AspNetUsers()
        {
            this.AspNetUserClaims = new HashSet<AspNetUserClaims>();
            this.AspNetUserLogins = new HashSet<AspNetUserLogins>();
            this.NursingHome = new HashSet<NursingHome>();
            this.AspNetRoles = new HashSet<AspNetRoles>();
        }

        //public string Id { get; set; }
        //public string UserName { get; set; }
        //public string PasswordHash { get; set; }
        //public string SecurityStamp { get; set; }
        public string Discriminator { get; set; }
        public System.Guid ApplicationId { get; set; }
        public string LegacyPasswordHash { get; set; }
        public string LoweredUserName { get; set; }
        public string MobileAlias { get; set; }
        public bool IsAnonymous { get; set; }
        public System.DateTime LastActivityDate { get; set; }
        public string MobilePIN { get; set; }
        public string Email { get; set; }
        public string LoweredEmail { get; set; }
        public string PasswordQuestion { get; set; }
        public string PasswordAnswer { get; set; }
        public bool IsApproved { get; set; }
        public bool IsLockedOut { get; set; }
        public System.DateTime CreateDate { get; set; }
        public System.DateTime LastLoginDate { get; set; }
        public System.DateTime LastPasswordChangedDate { get; set; }
        public System.DateTime LastLockoutDate { get; set; }
        public int FailedPasswordAttemptCount { get; set; }
        public System.DateTime FailedPasswordAttemptWindowStart { get; set; }
        public int FailedPasswordAnswerAttemptCount { get; set; }
        public System.DateTime FailedPasswordAnswerAttemptWindowStart { get; set; }
        public string Comment { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
        public virtual ICollection<AspNetUserLogins> AspNetUserLogins { get; set; }
        public virtual ICollection<NursingHome> NursingHome { get; set; }
        public virtual ICollection<AspNetRoles> AspNetRoles { get; set; }
    }
}

AspNetUsers 扩展

public partial class AspNetUsers : IdentityUser
{
}

AspNetRoles

public partial class AspNetRoles
{
    public AspNetRoles()
    {
        this.AspNetUsers = new HashSet<AspNetUsers>();
    }

    //public string Id { get; set; }
    //public string Name { get; set; }

    public virtual ICollection<AspNetUsers> AspNetUsers { get; set; }
}

AspNetRoles 扩展

public partial class AspNetRoles : IdentityRole
{
    public AspNetRoles(string name)
        : base(name)
    {
    }
}

数据库上下文扩展

public partial class Entities : IdentityDbContext<AspNetUsers>
{
    public Entities()
        : base("NursingHomesEntities")
    {
    }
}

更新

已安装的软件包

  • 安装包 Microsoft.AspNet.Identity.EntityFramework
  • 安装包 Microsoft.AspNet.Identity.Core
  • 安装包实体框架
  • 安装包 Microsoft.AspNet.Mvc
  • 安装包 Twitter.Bootstrap.MVC
  • 安装包 Microsoft.AspNet.Identity.Samples -Pre
  • Install-Package FontAwesome

接下来的步骤

  • 已添加 EDMX 文件(来自创建的数据库)
  • 添加了一张参考 AspNetUser 的表。

现在我收到以下错误:

Model compatibility cannot be checked because the DbContext instance was not created using Code First patterns. DbContext instances created from an ObjectContext or using an EDMX file cannot be checked for compatibility.

难道不能同时使用edmx文件和mvc5吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-5 asp.net-identity


    【解决方案1】:

    首先我推荐使用,一个新的asp.net identity 2.0 beta:https://www.nuget.org/packages/Microsoft.AspNet.Identity.Core/2.0.0-beta1

    重写和使用更好更灵活。你可以在这里获得一些信息、样本和其他东西:http://blogs.msdn.com/b/webdev/archive/2014/02/11/announcing-preview-of-microsoft-aspnet-identity-2-0-0-beta1.aspx

    我无法重现此错误,但可能是在映射配置中,实体框架错误而不是 asp.net MVC 错误.. 你需要小心点,替换你应用中所有使用的dbcontext,有一个话题可以看看以避免这个错误:The entity type <type> is not part of the model for the current context

    如果仍然无法正常工作,请尝试进行一些测试和体验或在此处添加更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 2012-05-22
      • 2011-01-15
      • 2018-01-26
      相关资源
      最近更新 更多