public static void EnsureSeeded(this EllegiaContext context, IServiceProvider serviceProvider, IConfigurationReader configurationReader) { var users = configurationReader.Read(); var userManager = serviceProvider.GetRequiredService <UserManager <EllegiaUser> >(); var roleManager = serviceProvider.GetRequiredService <RoleManager <EllegiaRole> >(); foreach (var user in users) { if (userManager.FindByNameAsync(user.Name).Result != null) { continue; } if (roleManager.FindByNameAsync(user.Role).Result == null) { var createRoleResult = roleManager.CreateAsync(new EllegiaRole { Name = user.Role }).Result; } var warehouse = context.Warehouses.First(); var ellegiaUser = new EllegiaUser(user.Name, user.Email, user.FullName, warehouse.Id); var createUserResult = userManager .CreateAsync(ellegiaUser, user.Password).Result; if (createUserResult.Succeeded) { var addUserToRoleResult = userManager.AddToRoleAsync(ellegiaUser, user.Role).Result; } } var ellegiaUsers = userManager.GetUsersInRoleAsync(Roles.SupervisorNormalizedName).Result; if (!context.Shifts.Any()) { context.Shifts.Add(new Shift("Смена #1", ellegiaUsers.First().Id)); context.SaveChanges(); } }
public CommandResponse Complete() { var rowsAffected = _context.SaveChanges(); return(new CommandResponse(rowsAffected > 0)); }