DbContextInitializer.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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, string filepath = null)
  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, filepath);
  41. }
  42. public static void SeedDatabase(DeathwatchDbContext context, string filesPath = null)
  43. {
  44. // Define the paths to the XML files
  45. //string accessoireFilePath = Path.Combine("XML", "Accessoires.xml");
  46. //string ameliorationFilePath = Path.Combine("XML", "Ameliorations.xml");
  47. //string extensionFilePath = Path.Combine("XML", "Extensions.xml");
  48. if (!string.IsNullOrEmpty(filesPath))
  49. {
  50. var roots = new List<Root>();
  51. try
  52. {
  53. // Deserialize the XML files
  54. GetRootsElements(roots, filesPath);
  55. ExtractFromRoot(context, roots);
  56. }
  57. catch (Exception e)
  58. {
  59. throw;
  60. }
  61. }
  62. }
  63. private static void GetRootsElements(List<Root> roots, string filesPath)
  64. {
  65. var fileExist = Directory.Exists(filesPath);
  66. if (fileExist)
  67. {
  68. var xmlFiles = Directory.GetFiles(filesPath, "*.xml");
  69. foreach (var xmlFile in xmlFiles)
  70. {
  71. var root = XmlHelper.DeserializeXmlFile<Root>(xmlFile);
  72. roots.Add(root);
  73. }
  74. }
  75. }
  76. private static void ExtractFromRoot(DeathwatchDbContext context, List<Root> roots)
  77. {
  78. foreach (var root in roots)
  79. {
  80. if (root != null)
  81. {
  82. if (root.Extensions != null && root.Extensions != null && !context.Extensions.Any())
  83. {
  84. context.Extensions.AddRange(root.Extensions);
  85. context.SaveChanges();
  86. }
  87. if (root.AccessoiresArmes != null && root.AccessoiresArmes != null && !context.Accessoires.Any())
  88. {
  89. context.Accessoires.AddRange(root.AccessoiresArmes);
  90. context.SaveChanges();
  91. }
  92. if (root.Améliorations != null && root.Améliorations != null && !context.Améliorations.Any())
  93. {
  94. context.Améliorations.AddRange(root.Améliorations);
  95. context.SaveChanges();
  96. }
  97. if (root.Archétypes != null && root.Archétypes != null && !context.Archétypes.Any())
  98. {
  99. context.Archétypes.AddRange(root.Archétypes);
  100. context.SaveChanges();
  101. }
  102. if (root.Armes != null && root.Armes != null && !context.Armes.Any())
  103. {
  104. context.Armes.AddRange(root.Armes);
  105. context.SaveChanges();
  106. }
  107. if (root.Armures != null && root.Armures != null && !context.Armures.Any())
  108. {
  109. context.Armures.AddRange(root.Armures);
  110. context.SaveChanges();
  111. }
  112. if (root.Assermentations != null && root.Assermentations != null && !context.Assermentations.Any())
  113. {
  114. context.Assermentations.AddRange(root.Assermentations);
  115. context.SaveChanges();
  116. }
  117. if (root.AttributsArmes != null && root.AttributsArmes != null && !context.Attributs.Any())
  118. {
  119. context.Attributs.AddRange(root.AttributsArmes);
  120. context.SaveChanges();
  121. }
  122. if (root.Carrières != null && root.Carrières != null && !context.Carrières.Any())
  123. {
  124. context.Carrières.AddRange(root.Carrières);
  125. context.SaveChanges();
  126. }
  127. if (root.Competences != null && root.Competences != null && !context.Competences.Any())
  128. {
  129. context.Competences.AddRange(root.Competences);
  130. context.SaveChanges();
  131. }
  132. if (root.Coordonnées != null && root.Coordonnées != null && !context.Coordonnées.Any())
  133. {
  134. context.Coordonnées.AddRange(root.Coordonnées);
  135. context.SaveChanges();
  136. }
  137. if (root.Cybernétiques != null && root.Cybernétiques != null && !context.Cybernétiques.Any())
  138. {
  139. context.Cybernétiques.AddRange(root.Cybernétiques);
  140. context.SaveChanges();
  141. }
  142. if (root.Disciplines != null && root.Disciplines != null && !context.Disciplines.Any())
  143. {
  144. context.Disciplines.AddRange(root.Disciplines);
  145. context.SaveChanges();
  146. }
  147. if (root.Divinations != null && root.Divinations != null && !context.Divinations.Any())
  148. {
  149. context.Divinations.AddRange(root.Divinations);
  150. context.SaveChanges();
  151. }
  152. if (root.Equipements != null && root.Equipements != null && !context.Equipements.Any())
  153. {
  154. context.Equipements.AddRange(root.Equipements);
  155. context.SaveChanges();
  156. }
  157. if (root.Implants != null && root.Implants != null && !context.Implants.Any())
  158. {
  159. context.Implants.AddRange(root.Implants);
  160. context.SaveChanges();
  161. }
  162. if (root.Mondes != null && root.Mondes != null && !context.Mondes.Any())
  163. {
  164. context.Mondes.AddRange(root.Mondes);
  165. context.SaveChanges();
  166. }
  167. if (root.Mutations != null && root.Mutations != null && !context.Mutations.Any())
  168. {
  169. context.Mutations.AddRange(root.Mutations);
  170. context.SaveChanges();
  171. }
  172. if (root.Pouvoirs != null && root.Pouvoirs != null && !context.Pouvoirs.Any())
  173. {
  174. context.Pouvoirs.AddRange(root.Pouvoirs);
  175. context.SaveChanges();
  176. }
  177. if (root.Promotions != null && root.Promotions != null && !context.Promotions.Any())
  178. {
  179. context.Promotions.AddRange(root.Promotions);
  180. context.SaveChanges();
  181. }
  182. if (root.SeuilsXp != null && root.SeuilsXp != null && !context.Seuils.Any())
  183. {
  184. context.Seuils.AddRange(root.SeuilsXp);
  185. context.SaveChanges();
  186. }
  187. if (root.Sousmondes != null && root.Sousmondes != null && !context.Sousmondes.Any())
  188. {
  189. context.Sousmondes.AddRange(root.Sousmondes);
  190. context.SaveChanges();
  191. }
  192. if (root.Talents != null && root.Talents != null && !context.Talents.Any())
  193. {
  194. context.Talents.AddRange(root.Talents);
  195. context.SaveChanges();
  196. }
  197. if (root.Traits != null && root.Traits != null && !context.Traits.Any())
  198. {
  199. context.Traits.AddRange(root.Traits);
  200. context.SaveChanges();
  201. }
  202. if (root.Troubles != null && root.Troubles != null && !context.Troubles.Any())
  203. {
  204. context.Troubles.AddRange(root.Troubles);
  205. context.SaveChanges();
  206. }
  207. if (root.TypeMondes != null && root.TypeMondes != null && !context.TypeMondes.Any())
  208. {
  209. context.TypeMondes.AddRange(root.TypeMondes);
  210. context.SaveChanges();
  211. }
  212. if (root.TypesNom != null && root.TypesNom != null && !context.TypesNom.Any())
  213. {
  214. context.TypesNom.AddRange(root.TypesNom);
  215. context.SaveChanges();
  216. }
  217. }
  218. }
  219. }
  220. }