首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >告诉UserManager使用AspNetRoles

告诉UserManager使用AspNetRoles
EN

Stack Overflow用户
提问于 2014-03-13 10:41:39
回答 1查看 1.6K关注 0票数 0

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

  • http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity
  • http://www.codeproject.com/Articles/682113/Extending-Identity-Accounts-and-Implementing-Role

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

当我试图更新用户的角色时,我得到了以下错误:

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

在此方法(um.AddToRole)中:

代码语言:javascript
复制
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类?

向你问好,马尔科

AspNetUsers

代码语言:javascript
复制
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扩展

代码语言:javascript
复制
public partial class AspNetUsers : IdentityUser
{
}

AspNetRoles

代码语言:javascript
复制
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扩展

代码语言:javascript
复制
public partial class AspNetRoles : IdentityRole
{
    public AspNetRoles(string name)
        : base(name)
    {
    }
}

DB上下文扩展

代码语言:javascript
复制
public partial class Entities : IdentityDbContext<AspNetUsers>
{
    public Entities()
        : base("NursingHomesEntities")
    {
    }
}

更新

已安装的软件包

  • 安装包Microsoft.AspNet.Identity.EntityFramework
  • 安装包Microsoft.AspNet.Identity.Core
  • 安装包EntityFramework
  • 安装包Microsoft.AspNet.Mvc
  • 安装包Twitter.Bootstrap.MVC
  • 安装包Microsoft.AspNet.Identity.Samples -Pre
  • 安装包FontAwesome

下一步

  • 添加EDMX文件(来自创建的数据库)
  • 添加了一个引用AspNetUser的表。

现在我得到了以下错误:

代码语言:javascript
复制
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吗?

EN

回答 1

Stack Overflow用户

发布于 2014-03-13 23:21:40

首先,我建议使用一个新的asp.net标识2.0测试版: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的所有用法,有一个主题要查看以避免此错误: is not part of the model for the current context

如果仍然不起作用,试着做一些测试和经验,或者在这里添加更多信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22376013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档