【问题标题】:ASP Identity - The entity type IdentityRole is not part of the model for the current contextASP Identity - 实体类型 IdentityRole 不是当前上下文模型的一部分
【发布时间】:2014-08-03 12:55:09
【问题描述】:

我扩展了身份模型,因此对于用户主键,我可以使用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

非常感谢您的帮助。

【问题讨论】:

  • 您确定在 StartupAuth.cs 中配置了正确的上下文吗?

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


【解决方案1】:

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

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

【讨论】:

  • 我遇到了同样的错误,但我只是在角色实体中添加了一个新字段。即使按照您发布的链接中的代码,也无法使其正常工作。 stackoverflow.com/questions/47351526/…
【解决方案2】:

由于您使用了 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>()));
    }
}

【讨论】:

    猜你喜欢
    • 2017-01-11
    • 2017-06-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    相关资源
    最近更新 更多