Advertisement
Guest User

Untitled

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