Advertisement
Guest User

Untitled

a guest
Sep 12th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. internal sealed class Configuration : DbMigrationsConfiguration<MiracleMachine.data.ApplicationDbContext>
  2. {
  3. public Configuration()
  4. {
  5. AutomaticMigrationsEnabled = false;
  6. }
  7.  
  8. protected override void Seed(MiracleMachine.data.ApplicationDbContext context)
  9. {
  10. context.Props.AddOrUpdate(
  11. p => p.PropId,
  12. new Prop() { PropName = "sharpie" },
  13. new Prop() { PropName = "coin" },
  14. new Prop() { PropName = "playing cards" },
  15. new Prop() { PropName = "coffee mug" },
  16. new Prop() { PropName = "phone" },
  17. new Prop() { PropName = "keys" },
  18. new Prop() { PropName = "sunglasses" },
  19. new Prop() { PropName = "headphones" },
  20. new Prop() { PropName = "ring" },
  21. new Prop() { PropName = "lighter" }
  22. );
  23. context.SaveChanges();
  24.  
  25. context.Theories.AddOrUpdate(
  26. t => t.TheoryId,
  27. new Theory()
  28. {
  29. // TheoryId = 0,
  30. TheoryName = "Production",
  31. TheoryDescription = "Make it appear out of nowhere!"
  32. },
  33. new Theory()
  34. {
  35. // TheoryId = 1,
  36. TheoryName = "Vanish",
  37. TheoryDescription = "Make it vanish into thin air!"
  38. },
  39. new Theory()
  40. {
  41. // TheoryId = 2,
  42. TheoryName = "Transportation",
  43. TheoryDescription = "Make it vanish, and then reappear somewhere impossible!"
  44. },
  45. new Theory()
  46. {
  47. // TheoryId = 3,
  48. TheoryName = "Transformation", // This uses TWO props
  49. TheoryDescription = "Cause one of these items to change into the other item!"
  50. },
  51. new Theory()
  52. {
  53. // TheoryId = 4,
  54. TheoryName = "Multiplication",
  55. TheoryDescription = "Magically duplicate this item again and again!"
  56. },
  57. new Theory()
  58. {
  59. // TheoryId = 5,
  60. TheoryName = "Penetration", // This uses TWO props
  61. TheoryDescription = "Cause the two items to inexplicably pass through each other"
  62. },
  63. new Theory()
  64. {
  65. // TheoryId = 6,
  66. TheoryName = "Restoration",
  67. TheoryDescription = "Destroy the item in some way. Restore it."
  68. },
  69. new Theory()
  70. {
  71. // TheoryId = 7,
  72. TheoryName = "Levitation",
  73. TheoryDescription = "Make the item float in mid-air!"
  74. });
  75. context.SaveChanges();
  76.  
  77. //////////////////////////////////////////// The following seeds user data
  78.  
  79. // ApplicationUser table seeder
  80. UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
  81. UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);
  82.  
  83. RoleStore<Role> roleStore = new RoleStore<Role>(context);
  84. RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);
  85.  
  86. if (!roleManager.RoleExists("Admin"))
  87. roleManager.Create(new Role { Name = "Admin" });
  88.  
  89. if (!roleManager.RoleExists("User"))
  90. roleManager.Create(new Role { Name = "User" });
  91.  
  92. IdentityResult result = null; // Sets the result to null. Used for error checking.
  93.  
  94. /////////// Admin (1)
  95. ApplicationUser admin1 = userManager.FindByName("MagicRawb");
  96.  
  97. if (admin1 == null)
  98. {
  99. admin1 = new ApplicationUser
  100. {
  101. FirstName = "Rob",
  102. LastName = "Greenlee",
  103. UserName = "magicrawb",
  104. Email = "magicrawb@test.com",
  105. Gender = Gender.Male
  106. };
  107. }
  108.  
  109. result = userManager.Create(admin1, "asdfasdf");
  110. if (!result.Succeeded)
  111. {
  112. string error = result.Errors.FirstOrDefault();
  113. throw new Exception(error);
  114. }
  115.  
  116. userManager.AddToRole(admin1.Id, "Admin"); // Add user1 to Admin role
  117. admin1 = userManager.FindByName("magicrawb"); // Assign user1 data to variable user1
  118.  
  119. /////////// Admin (2)
  120. ApplicationUser admin2 = userManager.FindByName("admin2");
  121.  
  122. if (admin2 == null)
  123. {
  124. admin2 = new ApplicationUser
  125. {
  126. FirstName = "Bekah",
  127. LastName = "Sells",
  128. UserName = "admin2",
  129. Email = "admin2@test.com",
  130. Gender = Gender.Female
  131. };
  132. }
  133.  
  134. result = userManager.Create(admin2, "asdfasdf");
  135. if (!result.Succeeded)
  136. {
  137. string error = result.Errors.FirstOrDefault();
  138. throw new Exception(error);
  139. }
  140.  
  141. userManager.AddToRole(admin2.Id, "Admin"); // Add user1 to Admin role
  142. admin1 = userManager.FindByName("admin2"); // Assign user1 data to variable user1
  143.  
  144. /////////// User (1)
  145. ApplicationUser user1 = userManager.FindByName("user1");
  146.  
  147. if (user1 == null)
  148. {
  149. user1 = new ApplicationUser
  150. {
  151. FirstName = "Lance",
  152. LastName = "Burton",
  153. UserName = "user1",
  154. Email = "user1@test.com",
  155. Gender = Gender.Male
  156. };
  157. }
  158.  
  159. result = userManager.Create(user1, "asdfasdf");
  160. if (!result.Succeeded)
  161. {
  162. string error = result.Errors.FirstOrDefault();
  163. throw new Exception(error);
  164. }
  165.  
  166. userManager.AddToRole(user1.Id, "User"); // Add user1 to Admin role
  167. user1 = userManager.FindByName("user1"); // Assign user1 data to variable user1
  168.  
  169. /////////// User (2)
  170. ApplicationUser user2 = userManager.FindByName("user2");
  171.  
  172. if (user2 == null)
  173. {
  174. user2 = new ApplicationUser
  175. {
  176. FirstName = "David",
  177. LastName = "Stone",
  178. UserName = "user2",
  179. Email = "user2@test.com",
  180. Gender = Gender.Male
  181. };
  182. }
  183.  
  184. result = userManager.Create(user2, "asdfasdf");
  185. if (!result.Succeeded)
  186. {
  187. string error = result.Errors.FirstOrDefault();
  188. throw new Exception(error);
  189. }
  190.  
  191. userManager.AddToRole(user1.Id, "User"); // Add user1 to Admin role
  192. user2 = userManager.FindByName("user2"); // Assign user1 data to variable user1
  193.  
  194. context.SaveChanges();
  195. Database.SetInitializer(new MyInitializer());
  196. }
  197. }
  198.  
  199. internal class MyInitializer : MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>
  200. {
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement