Guest User

Untitled

a guest
Mar 20th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
  2. {
  3. //will only try to seed if there is an outstanding migration
  4. if (!serviceScope.ServiceProvider.GetService<MyDataContext>().AllMigrationsApplied())
  5. {
  6. serviceScope.ServiceProvider.GetService<MyDataContext>().Database.Migrate();
  7. var roleManager = serviceScope.ServiceProvider.GetRequiredService<RoleManager<ApplicationRole>>();
  8. serviceScope.ServiceProvider.GetService<MyDataContext>().EnsureSeeded(roleManager);
  9. }
  10. }
  11.  
  12. public static class DbContextExtension
  13. {
  14.  
  15. public static bool AllMigrationsApplied(this MyDataContext context)
  16. {
  17. var applied = context.GetService<IHistoryRepository>()
  18. .GetAppliedMigrations()
  19. .Select(m => m.MigrationId);
  20.  
  21. var total = context.GetService<IMigrationsAssembly>()
  22. .Migrations
  23. .Select(m => m.Key);
  24.  
  25. return !total.Except(applied).Any();
  26. }
  27.  
  28. public static async void EnsureSeeded(this MyDataContext context, RoleManager<ApplicationRole> roleManager)
  29. {
  30. var buildDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  31. var filePath = buildDir + @"foo.txt";
  32.  
  33. if (!context.Clients.Any())
  34. {
  35. var clients = JsonConvert.DeserializeObject<List<Client>>(File.ReadAllText(@"seed" + Path.DirectorySeparatorChar + "clients.json"));
  36. context.AddRange(clients);
  37. context.SaveChanges();
  38. }
  39.  
  40. if (roleManager.Roles.Count() == 0)
  41. {
  42. string[] roleNames = { "Admin", "Manager", "Trader", "Public" };
  43.  
  44. IdentityResult roleResult;
  45.  
  46. foreach (var roleName in roleNames)
  47. {
  48. //creating the roles and seeding them to the database
  49. var roleExist = await roleManager.RoleExistsAsync(roleName);
  50. if (!roleExist)
  51. {
  52. roleResult = await roleManager.CreateAsync(new ApplicationRole(roleName));
  53. }
  54. }
  55. }
  56.  
  57. if (!context.Users.Any())
  58. {
  59. var user = new ApplicationUser
  60. {
  61. FirstName = "Matt",
  62. LastName = "Flynn",
  63. Email = "test@test.com",
  64. NormalizedEmail = "TEST@TEST.COM",
  65. UserName = "test@test.com",
  66. NormalizedUserName = "TEST@TEST.COM",
  67. PhoneNumber = "+441234567891",
  68. EmailConfirmed = true,
  69. PhoneNumberConfirmed = true,
  70. SecurityStamp = Guid.NewGuid().ToString("D")
  71. };
  72.  
  73. var password = new PasswordHasher<ApplicationUser>();
  74. var hashed = password.HashPassword(user, "password");
  75. user.PasswordHash = hashed;
  76. var userStore = new UserStore<ApplicationUser, ApplicationRole, MyDataContext, int>(context);
  77. await userStore.CreateAsync(user);
  78. await userStore.AddToRoleAsync(user, "Admin");
  79. await userStore.AddToRoleAsync(user, "Manager");
  80. await userStore.AddToRoleAsync(user, "Trader");
  81. await userStore.AddToRoleAsync(user, "Public");
  82.  
  83. }
  84.  
  85. }
  86. }
Add Comment
Please, Sign In to add comment