告诉UserManager使用AspNetRoles

时间:2014-03-13 10:41:40

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

我正在尝试将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类?

最诚挚的问候, 马尔科

AspNetUsers

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
  • Install-Package EntityFramework
  • 安装包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?

1 个答案:

答案 0 :(得分:0)

首先我建议使用新的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

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