I am getting weird results using Identity 3.0 seeding with integer ids. I am seeding roles - just two - using this:
using JobsLedger.DAL;
using JobsLedger.Models.Identity;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Identity;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
namespace JobsLedger.Models.Seeding
{
public static class SeedRoles
{
public static void EnsureRolesCreated(this IApplicationBuilder app)
{
var _context = app.ApplicationServices.GetService<JobsDbContext>();
if (_context.AllMigrationsApplied())
{
if (!_context.Roles.Any())
{
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).Result)
{
roleManager.CreateAsync(role);
}
}
_context.SaveChanges();
}
}
}
}
}
Which I got from the Unicorn example on github.
Role is as follows with an extra field called Description:
public class Role : IdentityRole<int>
{
public Role() : base() { }
public Role(string name) { Name = name; }
public string Description { get; set; }
}
In my startup I have this in the configure() method:
app.EnsureRolesCreated();
I have configured the project to recreate the database via Startup.cs using:
serviceScope.ServiceProvider.GetService<JobsDbContext>().Database.Migrate();
database gets created on startup if its not there... fine - it works.
Now when I check the roles I either get the second role only or both roles twice.. 4 records.. or, rarely just the two records which is what its suppose to be.
I've attempted to check for the roles in the method and also check if there are any roles before adding roles.
I have deleted the contents of the table so all rows and run it again and I get only the second role with an ID of "2"... seems ok that its id is two but why just one record?
deleted the database and re ran the project and now it has the second role with an id of "1"... but still just one record.
Rewrote the method to be closer to the Unicorn example thinking it might be the way I wrote the method..
public static void EnsureRolesCreated(this IApplicationBuilder app)
{
var _context = app.ApplicationServices.GetService<JobsDbContext>();
if (_context.AllMigrationsApplied())
{
if (!_context.Roles.Any())
{
var roleManager = app.ApplicationServices.GetService<RoleManager<Role>>();
var Roles = new string[,]
{
{"Admin","Users able to access all areas" },
{"Technician","Users able to access just the schedule" }
};
for (int i = 0; i < Roles.GetLength(0); i++)
{
if (!roleManager.RoleExistsAsync(Roles[i, 0].ToUpper()).Result)
{
roleManager.CreateAsync(new Role { Name = Roles[i, 0], Description = Roles[i, 1] });
}
}
}
}
}
It worked perfectly the first time I ran it.. deleted the database and ran it a second time.. 2nd record only was saved.. and then I deleted the database again and ran it a third time and got 4 records.. that's both roles twice??
Am I doing this incorrectly? The code appears to be OK for creating two roles but maybe I have missed something? Any ideas?