Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser, IdentityRole, and IdentityDbContext?

北城以北 提交于 2019-12-30 11:21:19

问题


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

回答1:


I'm still learning this new ASP.NET Identity system but have you tried omitting generic types like this:

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

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

same goes for ApplicationRole

MyAppDb would look like this:

public class MyAppDb: IdentityDbContext<ApplicationUser>

By default the IDs will be strings with auto-generated GUIDs in the DB




回答2:


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?



来源:https://stackoverflow.com/questions/22724938/is-there-a-way-to-create-a-custom-user-and-role-without-specifying-the-tkey-on-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!