有没有一种方法来创建自定义用户和角色,而无需指定有关IdenitityUser,IdentityRole和IdentityDbContext的TKEY的? [英] Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser, IdentityRole, and IdentityDbContext?

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

问题描述

有没有一种方法来创建自定义用户和角色,而不在 IdentityUser ,<$指定TKEY的字符串 C $ C> IdentityRole 和 IdentityDbContext ?我问,因为它似乎以为我不希望自动生成主键编号了,我绝对做的。做什么我做以下, UserManager.Create(用户名,密码)将在<$与 EntityValidationError 失败C $ C>编号。

 公共类ApplicationUser:IdentityUser&LT;字符串,ApplicationUserLogin,ApplicationUserRole,ApplicationUserClaim&GT;
{
    [需要]
    [StringLength(50)]
    公共字符串名字{获得;组; }    [需要]
    [StringLength(50)]
    公共字符串名字{获得;组; }
}公共类ApplicationUserLogin:IdentityUserLogin
{
}公共类ApplicationUserClaim:IdentityUserClaim
{
}公共类ApplicationUserRole:IdentityUserRole
{
}公共类ApplicationRole:IdentityRole&LT;字符串,ApplicationUserRole&GT;
{
    [需要]
    [StringLength(50)]
    公共字符串ProperName {搞定;组; }    [需要]
    公共字符串描述{搞定;组; }
}
公共类MyAppDb:IdentityDbContext&LT; ApplicationUser,ApplicationRole,串,ApplicationUserLogin,ApplicationUserRole,ApplicationUserClaim&GT;
{
    公共MyAppDb()
        :基地(MyAppDb)
    {
    }    公共静态MyAppDb的Create()
    {
        返回新MyAppDb();
    }
}


解决方案

看来,答案是的NO。如果指定的用户的TKEY的和/或角色不再为您创建的主键。

看来我是想事情过度复杂化。感谢@dima帮助我决定取消复杂的事情。这里是我做过什么成功地获得用户和角色(都与自定义属性)顺利进行,即成功地通过一个控制器和视图创建数据库中的记录:

更新:您可能想看看我在底部一个更好的/简单的解决方案中提供的链接

 公共类ApplicationUser:IdentityUser
{
    [需要]
    [StringLength(50)]
    公共字符串名字{获得;组; }    [需要]
    [StringLength(50)]
    公共字符串名字{获得;组; }
}//公共类ApplicationUserLogin:IdentityUserLogin
// {
//}//公共类ApplicationUserClaim:IdentityUserClaim
// {
//}//公共类ApplicationUserRole:IdentityUserRole
// {
//}公共类ApplicationRole:IdentityRole
{
    [需要]
    [StringLength(50)]
    公共字符串ProperName {搞定;组; }
}
公共类MyAppDb:IdentityDbContext&LT; ApplicationUser,ApplicationRole,串,IdentityUserLogin,IdentityUserRole,IdentityUserClaim&GT;
{
    公共MyAppDb()
        :基地(MyAppDb)
    {
    }
}

然而,一个新的问题出现了以的UserManager 。具体来说,我得到的误差的实体类型IdentityRole是不是该机型为当前上下文的一部分。在这一行code的 VAR身份=等待UserManager.CreateIdentityAsync(用户,DefaultAuthenticationTypes.ApplicationCookie);

更新:这里修正这个错误:为什么我收到一个IdentityRole错误?

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();
    }
}

解决方案

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.

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")
    {
    }
}

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);

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

这篇关于有没有一种方法来创建自定义用户和角色,而无需指定有关IdenitityUser,IdentityRole和IdentityDbContext的TKEY的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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