有没有办法在不指定 IdenitityUser、IdentityRole 和 IdentityDbContext 上的 TKey 的情况下创建自定义用户和角色? [英] Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser, IdentityRole, and IdentityDbContext?

查看:18
本文介绍了有没有办法在不指定 IdenitityUser、IdentityRole 和 IdentityDbContext 上的 TKey 的情况下创建自定义用户和角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法创建自定义用户和角色,而无需在 IdentityUserIdentityRoleIdentityDbContext 中指定 TKey string?我问是因为它似乎认为我不再需要自动生成的主键 Id 并且我绝对想要.执行我在下面所做的操作,UserManager.Create(user, password) 将失败,并在 Id 上显示 EntityValidationError.

Is there a way to create a custom User and Role without specifying the TKey string in IdentityUser, IdentityRole, and IdentityDbContext? I ask because it seems to think I don't want the auto-generated primary key Id anymore and I absolutely do. Doing what I've done below, UserManager.Create(user, password) will fail with an EntityValidationError on Id.

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

public class ApplicationUserLogin : IdentityUserLogin
{
}

public class ApplicationUserClaim : IdentityUserClaim
{
}

public class ApplicationUserRole : IdentityUserRole
{
}

public class ApplicationRole : IdentityRole<string, ApplicationUserRole>
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }

    [Required]
    public string Description { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public MyAppDb()
        : base("MyAppDb")
    {
    }

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

推荐答案

看来答案是NO".如果您在 User 和/或 Role 上指定 TKey,则不再为您创建主键.

It appears the answer is "NO". If you specify the TKey on User and/or Role the primary keys are no longer created for you.

看来我是想把事情复杂化.感谢@dima 帮助我决定让事情变得简单.这是我为成功使用户和角色(均具有自定义属性)成功工作所做的工作,即通过控制器和视图在数据库中成功创建记录:

It seems I was trying to over-complicate things. Thanks @dima for helping me decide to un-complicate things. Here is what I did to successfully get users and roles (both with custom properties) to successfully work, i.e., to successfully create records in the database via a controller and view:

更新:您可能想查看我在底部提供的链接以获得更好/更简单的解决方案.

UPDATE: You may want to look at the link I provided at the bottom for a better/simpler solution.

public class ApplicationUser : IdentityUser
{
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [Required]
    [StringLength(50)]
    public string LastName { get; set; }
}

//public class ApplicationUserLogin : IdentityUserLogin
//{
//}

//public class ApplicationUserClaim : IdentityUserClaim
//{
//}

//public class ApplicationUserRole : IdentityUserRole
//{
//}

public class ApplicationRole : IdentityRole
{
    [Required]
    [StringLength(50)]
    public string ProperName { get; set; }
}


public class MyAppDb : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
    public MyAppDb()
        : base("MyAppDb")
    {
    }
}

然而,UserManager 出现了一个新问题.具体来说,我收到错误 The entity type IdentityRole is not of the model for the current context. 在这行代码 var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

However, a new issue has arose with the UserManager. Specifically, I'm getting the error The entity type IdentityRole is not part of the model for the current context. on this line of code var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

更新:在此处修复此错误:为什么我是否收到 IdentityRole 错误?

UPDATE: Fixed this error here: Why am I getting an IdentityRole error?

这篇关于有没有办法在不指定 IdenitityUser、IdentityRole 和 IdentityDbContext 上的 TKey 的情况下创建自定义用户和角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆