DbContextInitializer.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. }
  73. if (root.AccessoiresArmes != null && root.AccessoiresArmes != null && !context.Accessoires.Any())
  74. {
  75. context.Accessoires.AddRange(root.AccessoiresArmes);
  76. }
  77. if (root.Améliorations != null && root.Améliorations != null && !context.Améliorations.Any())
  78. {
  79. context.Améliorations.AddRange(root.Améliorations);
  80. }
  81. if (root.Archétypes != null && root.Archétypes != null && !context.Archétypes.Any())
  82. {
  83. context.Archétypes.AddRange(root.Archétypes);
  84. }
  85. if (root.Armes != null && root.Armes != null && !context.Armes.Any())
  86. {
  87. context.Armes.AddRange(root.Armes);
  88. }
  89. if (root.Armures != null && root.Armures != null && !context.Armures.Any())
  90. {
  91. context.Armures.AddRange(root.Armures);
  92. }
  93. if (root.Assermentations != null && root.Assermentations != null && !context.Assermentations.Any())
  94. {
  95. context.Assermentations.AddRange(root.Assermentations);
  96. }
  97. if (root.AttributsArmes != null && root.AttributsArmes != null && !context.Attributs.Any())
  98. {
  99. context.Attributs.AddRange(root.AttributsArmes);
  100. }
  101. if (root.Carrières != null && root.Carrières != null && !context.Carrières.Any())
  102. {
  103. context.Carrières.AddRange(root.Carrières);
  104. }
  105. if (root.Competences != null && root.Competences != null && !context.Competences.Any())
  106. {
  107. context.Competences.AddRange(root.Competences);
  108. }
  109. if (root.Coordonnées != null && root.Coordonnées != null && !context.Coordonnées.Any())
  110. {
  111. context.Coordonnées.AddRange(root.Coordonnées);
  112. }
  113. if (root.Cybernétiques != null && root.Cybernétiques != null && !context.Cybernétiques.Any())
  114. {
  115. context.Cybernétiques.AddRange(root.Cybernétiques);
  116. }
  117. if (root.Disciplines != null && root.Disciplines != null && !context.Disciplines.Any())
  118. {
  119. context.Disciplines.AddRange(root.Disciplines);
  120. }
  121. if (root.Divinations != null && root.Divinations != null && !context.Divinations.Any())
  122. {
  123. context.Divinations.AddRange(root.Divinations);
  124. }
  125. if (root.Equipements != null && root.Equipements != null && !context.Equipements.Any())
  126. {
  127. context.Equipements.AddRange(root.Equipements);
  128. }
  129. if (root.Implants != null && root.Implants != null && !context.Implants.Any())
  130. {
  131. context.Implants.AddRange(root.Implants);
  132. }
  133. if (root.Mondes != null && root.Mondes != null && !context.Mondes.Any())
  134. {
  135. context.Mondes.AddRange(root.Mondes);
  136. }
  137. if (root.Mutations != null && root.Mutations != null && !context.Mutations.Any())
  138. {
  139. context.Mutations.AddRange(root.Mutations);
  140. }
  141. if (root.Pouvoirs != null && root.Pouvoirs != null && !context.Pouvoirs.Any())
  142. {
  143. context.Pouvoirs.AddRange(root.Pouvoirs);
  144. }
  145. if (root.Promotions != null && root.Promotions != null && !context.Promotions.Any())
  146. {
  147. context.Promotions.AddRange(root.Promotions);
  148. }
  149. if (root.SeuilsXp != null && root.SeuilsXp != null && !context.Seuils.Any())
  150. {
  151. context.Seuils.AddRange(root.SeuilsXp);
  152. }
  153. if (root.Sousmondes != null && root.Sousmondes != null && !context.Sousmondes.Any())
  154. {
  155. context.Sousmondes.AddRange(root.Sousmondes);
  156. }
  157. if (root.Talents != null && root.Talents != null && !context.Talents.Any())
  158. {
  159. context.Talents.AddRange(root.Talents);
  160. }
  161. if (root.Traits != null && root.Traits != null && !context.Traits.Any())
  162. {
  163. context.Traits.AddRange(root.Traits);
  164. }
  165. if (root.Troubles != null && root.Troubles != null && !context.Troubles.Any())
  166. {
  167. context.Troubles.AddRange(root.Troubles);
  168. }
  169. if (root.TypeMondes != null && root.TypeMondes != null && !context.TypeMondes.Any())
  170. {
  171. context.TypeMondes.AddRange(root.TypeMondes);
  172. }
  173. if (root.TypesNom != null && root.TypesNom != null && !context.TypesNom.Any())
  174. {
  175. context.TypesNom.AddRange(root.TypesNom);
  176. }
  177. }
  178. }
  179. context.SaveChanges();
  180. }
  181. }