I am using Identity 3. I want to seed an admin user on startup.
I have converted the string ids to "int" ids using as an example SwiftCourier on github. Identity 3 is different in some aspects and this applies to the setting of "int" for ids as shown in the SwiftCourier example as compared to Identity 2 and the setting of "int" ids.
I've done this by placing "int" after my user:
public class User : IdentityUser<int>{...}
After my Role:
public class Role : IdentityRole<int>
and in startup.cs in configuration services I entered this:
services.AddIdentity<User, Role>(options => options.User.AllowedUserNameCharacters = null)
.AddEntityFrameworkStores<JobsDbContext, int>()
.AddUserStore<UserStore<User, Role, JobsDbContext, int>>()
.AddRoleStore<RoleStore<Role, JobsDbContext, int>>()
.AddDefaultTokenProviders();
I then used this to seed roles and it works with integer ids:
public static void EnsureRolesCreated(this IApplicationBuilder app)
{
var context = app.ApplicationServices.GetService<JobsDbContext>();
if (context.AllMigrationsApplied())
{
var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>();
var Roles = new List<Role>();
Roles.Add(new Role { Name = "Admin", Description = "Users able to access all areas" });
Roles.Add(new Role { Name = "Technician", Description = "Users able to access just the schedule" });
foreach (var role in Roles)
{
if (!roleManager.RoleExistsAsync(role.Name.ToUpper()).Result)
{
roleManager.CreateAsync(role);
}
}
}
}
So far so good... I want to seed just one admin user to start with and this is where I fail...
I have used the following Stackoverflow example by @Guy.
I am struggling with UserStore with this line:
await new UserStore<User>(context).CreateAsync(userWithRoles.User);
It fails on "User" which is my ApplicationUser just renamed.
The error I get is:
The type 'JobsLedger.Models.Identity.User' cannot be used as type parameter 'TUser' in the generic type or method 'UserStore<TUser>'. There is no implicit reference conversion from 'JobsLedger.Models.Identity.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser<string>'.
It seems to be complaining on the fact that the id is "int" I think
How do I make the UserStore work with an "int"in this regard?? Its set to int in the Startup.cs as shown above..
If I have to create a custom "UserStore" that works with "int".. how do you do it in Identity 3?