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?