Advertisement
Guest User

AppDatabaseInitialization.cs

a guest
Aug 3rd, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.34 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 SeedAsync(ApplicationDbContext context)
  16.         {
  17.  
  18.             InitializeRoles(context);
  19.             InitializeUsers(context);
  20.             InitializeBooks(context);
  21.         }
  22.  
  23.         private static void 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.             CreateUserAsync(user1, userManager);
  33.             CreateUserAsync(user2, userManager);
  34.             CreateUserAsync(user3, userManager);
  35.         }
  36.  
  37.         private static async void CreateUserAsync(ApplicationUser user, UserManager<ApplicationUser> userManager)
  38.         {
  39.             var appUser = await userManager.FindByEmailAsync(user.Email);
  40.             if (appUser != null)
  41.             {
  42.                 await userManager.CreateAsync(appUser, "Test123@");
  43.             }
  44.             return;
  45.         }
  46.  
  47.         private static void InitializeBooks(ApplicationDbContext context)
  48.         {
  49.             List<Book> books = new List<Book>() {
  50.                 new Book() { Id = 1, Name = "The Alchemist", Authors = "Paulo Coelho" },
  51.                 new Book() { Id = 2, Name = "The Very Hungry Caterpillar", Authors = "Eric Carie" },
  52.                 new Book() { Id = 3, Name = "Goodnight Moon", Authors = "Margaret Wise Brown" },
  53.                 new Book() { Name = "The Outsider: A Novel", Authors = "Stephen King" },
  54.                 new Book() { Name = "Just Mercy: A Story of Justice and Redemption", Authors = "Bryan Stevenson" }
  55.             };
  56.  
  57.             context.Books.AddRange(books);
  58.             context.SaveChanges();
  59.         }
  60.  
  61.         private static void InitializeRoles(ApplicationDbContext context)
  62.         {
  63.             var roles = context.Roles.ToList();
  64.  
  65.             var adminRole = new IdentityRole("Administrator");
  66.             var editorRole = new IdentityRole("Editor");
  67.             var standardUserRole = new IdentityRole("StandardUser");
  68.  
  69.             if (roles == null || roles.Count == 0)
  70.             {
  71.  
  72.                 context.Roles.Add(adminRole);
  73.                 context.Roles.Add(editorRole);
  74.                 context.Roles.Add(standardUserRole);
  75.  
  76.             } else {
  77.  
  78.                 if (!roles.Any(role => role.Name == "Administrator")) context.Roles.Add(adminRole);
  79.                 if (!roles.Any(role => role.Name == "Editor")) context.Roles.Add(editorRole);
  80.                 if (!roles.Any(role => role.Name == "StandardUser")) context.Roles.Add(standardUserRole);
  81.  
  82.             }
  83.  
  84.             context.SaveChanges();
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement