Advertisement
Guest User

AppDatabaseInitializer.cs

a guest
Aug 3rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using Microsoft.AspNet.Identity;
  2. using Microsoft.AspNet.Identity.EntityFramework;
  3. using SharedLibrary.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data.Entity;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10.  
  11. namespace SharedLibrary.Utils
  12. {
  13.     public class AppDatabaseInitializator
  14.     {
  15.         public static async void Seed(ApplicationDbContext context)
  16.         {
  17.  
  18.             InitializeRoles(context);
  19.             await InitializeUsers(context);
  20.             InitializeBooks(context);
  21.         }
  22.  
  23.         private static async Task InitializeUsers(ApplicationDbContext context)
  24.         {
  25.             var userStore = new UserStore<ApplicationUser>(context);
  26.             var userManager = new ApplicationUserManager(userStore);
  27.  
  28.             var user1 = new ApplicationUser() { FirstName = "Mitre", LastName = "Mitrevski", Email = "mitre.mitrevski@gmail.com" };
  29.             var user2 = new ApplicationUser() { FirstName = "Petre", LastName = "Petrevski", Email = "petre.petrevski@gmail.com" };
  30.             var user3 = new ApplicationUser() { FirstName = "Stanko", LastName = "Stankovski", Email = "stanko.stankovski@gmail.com" };
  31.  
  32.             try
  33.             {
  34.                 await CreateUserAsync(user1, userManager);
  35.                 await CreateUserAsync(user2, userManager);
  36.                 await CreateUserAsync(user3, userManager);
  37.             } catch(Exception exc)
  38.             {
  39.                 throw exc;
  40.             }
  41.         }
  42.  
  43.         private static async Task CreateUserAsync(ApplicationUser user, UserManager<ApplicationUser> userManager)
  44.         {
  45.             var appUser = await userManager.FindByEmailAsync(user.Email);
  46.             if (appUser == null)
  47.             {
  48.                 IdentityResult result = await userManager.CreateAsync(user, "Test123@");
  49.  
  50.                 if (!result.Succeeded)
  51.                     throw new Exception(result.Errors.ToString());
  52.             }
  53.             return;
  54.         }
  55.  
  56.         private static void InitializeBooks(ApplicationDbContext context)
  57.         {
  58.             List<Book> books = new List<Book>() {
  59.                 new Book() { Id = 1, Name = "The Alchemist", Authors = "Paulo Coelho" },
  60.                 new Book() { Id = 2, Name = "The Very Hungry Caterpillar", Authors = "Eric Carie" },
  61.                 new Book() { Id = 3, Name = "Goodnight Moon", Authors = "Margaret Wise Brown" },
  62.                 new Book() { Name = "The Outsider: A Novel", Authors = "Stephen King" },
  63.                 new Book() { Name = "Just Mercy: A Story of Justice and Redemption", Authors = "Bryan Stevenson" }
  64.             };
  65.  
  66.             context.Books.AddRange(books);
  67.             context.SaveChanges();
  68.         }
  69.  
  70.         private static void InitializeRoles(ApplicationDbContext context)
  71.         {
  72.             var roles = context.Roles.ToList();
  73.  
  74.             var adminRole = new IdentityRole("Administrator");
  75.             var editorRole = new IdentityRole("Editor");
  76.             var standardUserRole = new IdentityRole("StandardUser");
  77.  
  78.             if (roles == null || roles.Count == 0)
  79.             {
  80.  
  81.                 context.Roles.Add(adminRole);
  82.                 context.Roles.Add(editorRole);
  83.                 context.Roles.Add(standardUserRole);
  84.  
  85.             } else {
  86.  
  87.                 if (!roles.Any(role => role.Name == "Administrator")) context.Roles.Add(adminRole);
  88.                 if (!roles.Any(role => role.Name == "Editor")) context.Roles.Add(editorRole);
  89.                 if (!roles.Any(role => role.Name == "StandardUser")) context.Roles.Add(standardUserRole);
  90.  
  91.             }
  92.  
  93.             context.SaveChanges();
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement