ASP标识 - 实体类型IdentityRole不是当前上下文的模型的一部分

时间:2014-08-03 12:55:10

标签: c# asp.net-mvc asp.net-identity

我扩展了身份模型,因此我可以针对用户主键设置Int而不是string

namespace BeyondMembership.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
      ...
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
        int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {

        }

        static ApplicationDbContext()
        {
            // Set the database intializer which is run once during application start
            // This seeds the database with admin user credentials and admin role
            Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }

public class CustomRole : IdentityRole<int, CustomUserRole>
{
    public CustomRole() { }
    public CustomRole(string name) { Name = name; }
}

public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
    CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public CustomUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
    public CustomRoleStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

所以我在roleManager.FindByName(roleName);上收到错误:

//Create User=Admin@Admin.com with password=Admin@123456 in the Admin role        
public static void InitializeIdentityForEF(ApplicationDbContext db)
{
    var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
    var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
    var role = roleManager.FindByName(roleName); // The entity type IdentityRole is not part of the model for the current context

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

您可能缺少某些实现。看一下这个示例,它已经使用Int作为主键,并执行您需要做的事情。

https://github.com/TypecastException/AspNet-Identity-2-With-Integer-Keys

答案 1 :(得分:1)

由于您使用了CustomRole - 您也应该应用所有其他地方,希望您明白这一点。

public class ApplicationRoleManager : RoleManager<CustomRole,int>
{
    public ApplicationRoleManager(IRoleStore<CustomRole,int> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        //return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
        return new ApplicationRoleManager(new CustomRoleStore(context.Get<ApplicationDbContext>()));
    }
}