DbContextInitializer.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using Domain;
  2. using Domain.Weapons;
  3. using Domain.XML_Model;
  4. using Infrastructure;
  5. using System.Collections.Generic;
  6. namespace DataLayer;
  7. public static class DbContextInitializer
  8. {
  9. public static void Initialize(DeathwatchDbContext context)
  10. {
  11. context.Database.EnsureCreated();
  12. // Look for any students.
  13. if (context.Players.Any()) return; // DB has been seeded
  14. var weapon = new Weapon
  15. {
  16. At = "test",
  17. Range = 30,
  18. Attribute = "CT",
  19. Type = "RANGE"
  20. };
  21. var players = new[]
  22. {
  23. new Player
  24. {
  25. Experience = 17400,
  26. CC = 52,
  27. CT = 40,
  28. F = 46,
  29. E = 45,
  30. AG = 50,
  31. INT = 34,
  32. PER = 43,
  33. FM = 43,
  34. SOC = 40,
  35. Weapons = new List<Weapon> { weapon }
  36. }
  37. };
  38. foreach (var player in players) context.Players.Add(player);
  39. context.SaveChanges();
  40. SeedDatabase(context);
  41. }
  42. public static void SeedDatabase(DeathwatchDbContext context)
  43. {
  44. // Define the paths to the XML files
  45. var parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent;
  46. var folderPath = Path.Combine($"{parent}", "Dotnet", "DataLayer", "XML");
  47. var xmlFiles = Directory.GetFiles(folderPath, "*.xml");
  48. //string accessoireFilePath = Path.Combine("XML", "Accessoires.xml");
  49. //string ameliorationFilePath = Path.Combine("XML", "Ameliorations.xml");
  50. //string extensionFilePath = Path.Combine("XML", "Extensions.xml");
  51. var roots = new List<Root>();
  52. try
  53. {
  54. foreach (var xmlFile in xmlFiles)
  55. {
  56. var root = XmlHelper.DeserializeXmlFile<Root>(xmlFile);
  57. roots.Add(root);
  58. }
  59. }
  60. catch (Exception e)
  61. {
  62. throw;
  63. }
  64. // Deserialize the XML files
  65. foreach (var root in roots)
  66. {
  67. if (root != null)
  68. {
  69. if (root.Extensions != null && root.Extensions != null && !context.Extensions.Any())
  70. {
  71. context.Extensions.AddRange(root.Extensions);
  72. context.SaveChanges();
  73. }
  74. if (root.AccessoiresArmes != null && root.AccessoiresArmes != null && !context.Accessoires.Any())
  75. {
  76. context.Accessoires.AddRange(root.AccessoiresArmes);
  77. context.SaveChanges();
  78. }
  79. if (root.Améliorations != null && root.Améliorations != null && !context.Améliorations.Any())
  80. {
  81. context.Améliorations.AddRange(root.Améliorations);
  82. context.SaveChanges();
  83. }
  84. if (root.Archétypes != null && root.Archétypes != null && !context.Archétypes.Any())
  85. {
  86. context.Archétypes.AddRange(root.Archétypes);
  87. context.SaveChanges();
  88. }
  89. if (root.Armes != null && root.Armes != null && !context.Armes.Any())
  90. {
  91. context.Armes.AddRange(root.Armes);
  92. context.SaveChanges();
  93. }
  94. if (root.Armures != null && root.Armures != null && !context.Armures.Any())
  95. {
  96. context.Armures.AddRange(root.Armures);
  97. context.SaveChanges();
  98. }
  99. if (root.Assermentations != null && root.Assermentations != null && !context.Assermentations.Any())
  100. {
  101. context.Assermentations.AddRange(root.Assermentations);
  102. context.SaveChanges();
  103. }
  104. if (root.AttributsArmes != null && root.AttributsArmes != null && !context.Attributs.Any())
  105. {
  106. context.Attributs.AddRange(root.AttributsArmes);
  107. context.SaveChanges();
  108. }
  109. if (root.Carrières != null && root.Carrières != null && !context.Carrières.Any())
  110. {
  111. context.Carrières.AddRange(root.Carrières);
  112. context.SaveChanges();
  113. }
  114. if (root.Competences != null && root.Competences != null && !context.Competences.Any())
  115. {
  116. context.Competences.AddRange(root.Competences);
  117. context.SaveChanges();
  118. }
  119. if (root.Coordonnées != null && root.Coordonnées != null && !context.Coordonnées.Any())
  120. {
  121. context.Coordonnées.AddRange(root.Coordonnées);
  122. context.SaveChanges();
  123. }
  124. if (root.Cybernétiques != null && root.Cybernétiques != null && !context.Cybernétiques.Any())
  125. {
  126. context.Cybernétiques.AddRange(root.Cybernétiques);
  127. context.SaveChanges();
  128. }
  129. if (root.Disciplines != null && root.Disciplines != null && !context.Disciplines.Any())
  130. {
  131. context.Disciplines.AddRange(root.Disciplines);
  132. context.SaveChanges();
  133. }
  134. if (root.Divinations != null && root.Divinations != null && !context.Divinations.Any())
  135. {
  136. context.Divinations.AddRange(root.Divinations);
  137. context.SaveChanges();
  138. }
  139. if (root.Equipements != null && root.Equipements != null && !context.Equipements.Any())
  140. {
  141. context.Equipements.AddRange(root.Equipements);
  142. context.SaveChanges();
  143. }
  144. if (root.Implants != null && root.Implants != null && !context.Implants.Any())
  145. {
  146. context.Implants.AddRange(root.Implants);
  147. context.SaveChanges();
  148. }
  149. if (root.Mondes != null && root.Mondes != null && !context.Mondes.Any())
  150. {
  151. context.Mondes.AddRange(root.Mondes);
  152. context.SaveChanges();
  153. }
  154. if (root.Mutations != null && root.Mutations != null && !context.Mutations.Any())
  155. {
  156. context.Mutations.AddRange(root.Mutations);
  157. context.SaveChanges();
  158. }
  159. if (root.Pouvoirs != null && root.Pouvoirs != null && !context.Pouvoirs.Any())
  160. {
  161. context.Pouvoirs.AddRange(root.Pouvoirs);
  162. context.SaveChanges();
  163. }
  164. if (root.Promotions != null && root.Promotions != null && !context.Promotions.Any())
  165. {
  166. context.Promotions.AddRange(root.Promotions);
  167. context.SaveChanges();
  168. }
  169. if (root.SeuilsXp != null && root.SeuilsXp != null && !context.Seuils.Any())
  170. {
  171. context.Seuils.AddRange(root.SeuilsXp);
  172. context.SaveChanges();
  173. }
  174. if (root.Sousmondes != null && root.Sousmondes != null && !context.Sousmondes.Any())
  175. {
  176. context.Sousmondes.AddRange(root.Sousmondes);
  177. context.SaveChanges();
  178. }
  179. if (root.Talents != null && root.Talents != null && !context.Talents.Any())
  180. {
  181. context.Talents.AddRange(root.Talents);
  182. context.SaveChanges();
  183. }
  184. if (root.Traits != null && root.Traits != null && !context.Traits.Any())
  185. {
  186. context.Traits.AddRange(root.Traits);
  187. context.SaveChanges();
  188. }
  189. if (root.Troubles != null && root.Troubles != null && !context.Troubles.Any())
  190. {
  191. context.Troubles.AddRange(root.Troubles);
  192. context.SaveChanges();
  193. }
  194. if (root.TypeMondes != null && root.TypeMondes != null && !context.TypeMondes.Any())
  195. {
  196. context.TypeMondes.AddRange(root.TypeMondes);
  197. context.SaveChanges();
  198. }
  199. if (root.TypesNom != null && root.TypesNom != null && !context.TypesNom.Any())
  200. {
  201. context.TypesNom.AddRange(root.TypesNom);
  202. context.SaveChanges();
  203. }
  204. }
  205. }
  206. }
  207. }