Advertisement
Guest User

Initializer.cs

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