Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.09 KB | None | 0 0
  1. namespace EventManagement.Migrations
  2. {
  3.     using EventsManagement.Models;
  4.     using Microsoft.AspNet.Identity;
  5.     using Microsoft.AspNet.Identity.EntityFramework;
  6.     using System;
  7.     using System.Data.Entity;
  8.     using System.Data.Entity.Migrations;
  9.     using System.Linq;
  10.  
  11.     internal sealed class Configuration : DbMigrationsConfiguration<EventsManagement.Models.AppContext>
  12.     {
  13.         public Configuration()
  14.         {
  15.             AutomaticMigrationsEnabled = false;
  16.         }
  17.  
  18.         protected override void Seed(EventsManagement.Models.AppContext context)
  19.         {
  20.             //  This method will be called after migrating to the latest version.
  21.  
  22.             //  You can use the DbSet<T>.AddOrUpdate() helper extension method
  23.             //  to avoid creating duplicate seed data.
  24.  
  25.             var states = new[]
  26.             {
  27.                 new State() {Name = "oczekujące"},
  28.                 new State() {Name = "anulowane"},
  29.                 new State() {Name = "zrealizowane"},
  30.                 new State() {Name = "niezrealizowane"}
  31.             };
  32.  
  33.             context.States.AddOrUpdate(
  34.                 s => s.Name,
  35.                 states
  36.             );
  37.  
  38.             var organizationalUnits = new[]
  39.             {
  40.                 new OrganizationalUnit() {Name = "Dział IT"},
  41.                 new OrganizationalUnit() {Name = "Dział Public Relations"},
  42.                 new OrganizationalUnit() {Name = "Dział Finansów"},
  43.                 new OrganizationalUnit() {Name = "Dział Księgowości"},
  44.                 new OrganizationalUnit() {Name = "Dział Prawny"},
  45.                 new OrganizationalUnit() {Name = "Dział HR"},
  46.                 new OrganizationalUnit() {Name = "Dział Kadr"},
  47.                 new OrganizationalUnit() {Name = "Dział Logistyki"},
  48.                 new OrganizationalUnit() {Name = "Dział Produkcji"},
  49.                 new OrganizationalUnit() {Name = "Dział Handlowy"},
  50.             };
  51.  
  52.             context.OrganizationalUnits.AddOrUpdate(
  53.                 o => o.Name,
  54.                 organizationalUnits
  55.             );
  56.  
  57.             var employee = new IdentityRole("employee");
  58.             var admin = new IdentityRole("admin");
  59.  
  60.             context.Roles.AddOrUpdate(
  61.                 r => r.Name,
  62.                 employee,
  63.                 admin
  64.             );
  65.             context.SaveChanges();
  66.  
  67.             var employeeMail = "pracownik@mail.com";
  68.             var employee2Mail = "pracownik2@mail.com";
  69.             var adminMail = "admin@mail.com";
  70.  
  71.  
  72.             var userE = context.Users.SingleOrDefault(x => x.UserName == employeeMail);
  73.             var userE2 = context.Users.SingleOrDefault(x => x.UserName == employee2Mail);
  74.             var userA = context.Users.SingleOrDefault(x => x.UserName == adminMail);
  75.  
  76.             var hasher = new PasswordHasher();
  77.             var pass = "qwerty";
  78.             var passHash = hasher.HashPassword(pass);
  79.  
  80.  
  81.             if (userE == null)
  82.             {
  83.                 userE = new User
  84.                 {
  85.                     Name = "Adrian",
  86.                     Surname = "Rodzoń",
  87.                     Email = employeeMail,
  88.                     EmailConfirmed = true,
  89.                     PasswordHash = passHash,
  90.                     SecurityStamp = new Guid().ToString(),
  91.                     UserName = employeeMail,
  92.                     OrganizationalUnitId = organizationalUnits[0].Id,
  93.  
  94.                 };
  95.                 userE.Roles.Add(new IdentityUserRole() { RoleId = employee.Id, UserId = userE.Id });
  96.                 context.Users.Add(userE);
  97.             }
  98.  
  99.             if (userE2 == null)
  100.             {
  101.                 userE2 = new User
  102.                 {
  103.                     Name = "Tomasz",
  104.                     Surname = "Rzucidlak",
  105.                     Email = employee2Mail,
  106.                     EmailConfirmed = true,
  107.                     PasswordHash = passHash,
  108.                     SecurityStamp = new Guid().ToString(),
  109.                     UserName = employee2Mail,
  110.                     OrganizationalUnitId = organizationalUnits[1].Id,
  111.  
  112.                 };
  113.                 userE2.Roles.Add(new IdentityUserRole() { RoleId = employee.Id, UserId = userE2.Id });
  114.                 context.Users.Add(userE2);
  115.             }
  116.  
  117.             if (userA == null)
  118.             {
  119.                 userA = new User
  120.                 {
  121.                     Surname = "Admiński",
  122.                     Email = adminMail,
  123.                     EmailConfirmed = true,
  124.                     PasswordHash = passHash,
  125.                     SecurityStamp = new Guid().ToString(),
  126.                     Name = "Admin",
  127.                     UserName = adminMail,
  128.                     OrganizationalUnitId = organizationalUnits[0].Id,
  129.                 };
  130.                 userA.Roles.Add(new IdentityUserRole() { RoleId = admin.Id, UserId = userA.Id });
  131.                 //context.Users.Add(userA);
  132.                 context.Users.AddOrUpdate(
  133.                 r => r.UserName,
  134.                 userE, userE2, userA);
  135.             }
  136.  
  137.  
  138.             context.SaveChanges();
  139.  
  140.             var events = new Event[]
  141.             {
  142.                 new Event
  143.                 {
  144.                     Title = "Spotkanie organizacyjne",
  145.                     Description = "Opis 1",
  146.                     Place = "Pokój 109",
  147.                     EventDate = DateTime.Now,
  148.                     EventTime = DateTime.Now,
  149.                     EventAdd = DateTime.Parse("2008-08-01"),
  150.                     AuthorId = userE.Id,
  151.                     StateId = states[0].Id
  152.                 },
  153.                 new Event
  154.                 {
  155.                     Title = "Projekt NTI",
  156.                     Description = "Opis 2",
  157.                     Place = "Sala konferencyjna",
  158.                     EventDate = DateTime.Now,
  159.                     EventTime = DateTime.Now,
  160.                     EventAdd = DateTime.Parse("2011-11-11"),
  161.                     AuthorId = userE2.Id,
  162.                     StateId = states[1].Id
  163.                 },
  164.                 new Event
  165.                 {
  166.                     Title = "Rekrutacja",
  167.                     Description = "Opis 3",
  168.                     Place = "Pokój 110",
  169.                     EventDate = DateTime.Now,
  170.                     EventTime = DateTime.Now,
  171.                     EventAdd = DateTime.Parse("2013-08-01"),
  172.                     AuthorId = userE.Id,
  173.                     StateId = states[2].Id
  174.                 },
  175.                 new Event
  176.                 {
  177.                     Title = "Podsumowanie ostatniego miesiąca",
  178.                     Description = "Opis 4",
  179.                     Place = "Sala konferencyjna",
  180.                     EventDate = DateTime.Now,
  181.                     EventTime = DateTime.Now,
  182.                     EventAdd = DateTime.Parse("2011-08-01"),
  183.                     AuthorId = userE.Id,
  184.                     StateId = states[3].Id
  185.                 },
  186.             };
  187.  
  188.             context.SaveChanges();
  189.  
  190.             context.Events.AddOrUpdate(
  191.                 r => r.Title,
  192.                 events
  193.             );
  194.  
  195.             context.SaveChanges();
  196.  
  197.  
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement