瀏覽代碼

issue with tracking solved

Florian Morel 1 年之前
父節點
當前提交
079487a2bf

+ 6 - 6
Dotnet/DataLayer/AbstractRepository.cs

@@ -7,6 +7,12 @@ namespace DataLayer;
 
 public class AbstractRepository<T> : IAbstractRepository<T> where T : BaseEntity
 {
+    public AbstractRepository(DeathwatchDbContext deathwatchDbContext)
+    {
+        _deathwatchDbContext = deathwatchDbContext;
+        _entities = _deathwatchDbContext.Set<T>();
+    }
+
     #region Fields
 
     private readonly DeathwatchDbContext _deathwatchDbContext;
@@ -14,12 +20,6 @@ public class AbstractRepository<T> : IAbstractRepository<T> where T : BaseEntity
 
     #endregion
 
-    public AbstractRepository(DeathwatchDbContext deathwatchDbContext)
-    {
-        _deathwatchDbContext = deathwatchDbContext;
-        _entities = _deathwatchDbContext.Set<T>();
-    }
-
     #region Public Methods
 
     public async Task<T> GetById(int id)

+ 1 - 7
Dotnet/DataLayer/DataLayer.csproj

@@ -21,10 +21,4 @@
     <ProjectReference Include="..\Domain\Domain.csproj" />
   </ItemGroup>
 
-  <ItemGroup>
-    <None Update="XMLTEST\01 - ldb.xml">
-      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
-    </None>
-  </ItemGroup>
-
-</Project>
+</Project>

+ 89 - 181
Dotnet/DataLayer/DbContextInitializer.cs

@@ -2,21 +2,23 @@
 using Domain.Weapons;
 using Domain.XML_Model;
 using Infrastructure;
-using System.Collections.Generic;
+using Microsoft.EntityFrameworkCore;
+using System.Numerics;
 
 namespace DataLayer;
 
 public static class DbContextInitializer
 {
-    public static void Initialize(DeathwatchDbContext context, string filepath = null)
+    private static bool _isSeeded = false;
+    public static async Task Initialize(DeathwatchDbContext context, string? filepath = null)
     {
-        context.Database.EnsureCreated();
-        SeedDatabase(context, filepath);
+        await context.Database.EnsureCreatedAsync();
+        await SeedDatabase(context, filepath);
     }
 
-    private static bool SeedPlayers(DeathwatchDbContext context)
+    private static async Task SeedPlayers(DeathwatchDbContext context)
     {
-        if (context.Players.Any()) return true;
+        if (context.Players.Any()) return;
         var players = new[]
         {
             new Player
@@ -31,28 +33,32 @@ public static class DbContextInitializer
                 PER = 43,
                 FM = 43,
                 SOC = 40,
-                Weapons = new List<Weapon> { new()
+                Weapons = new List<Weapon>
                 {
-                    At = "test",
-                    Range = 30,
-                    Attribute = "CT",
-                    Type = "RANGE"
-                }}
+                    new()
+                    {
+                        At = "test",
+                        Range = 30,
+                        Attribute = "CT",
+                        Type = "RANGE"
+                    }
+                }
             }
         };
-        foreach (var player in players) context.Players.Add(player);
-        context.SaveChanges();
-        return false;
+        foreach (var player in players) await context.Players.AddAsync(player);
+        await context.SaveChangesAsync();
     }
 
-    public static void SeedDatabase(DeathwatchDbContext context, string filesPath = null)
+    public static async Task SeedDatabase(DeathwatchDbContext context, string? filesPath = null)
     {
-        if (SeedPlayers(context)) return;
+        if (_isSeeded) return;
+        await SeedPlayers(context);
 
         if (string.IsNullOrEmpty(filesPath)) return;
         var roots = new List<Root>();
         GetRootsElements(roots, filesPath);
-        ExtractFromRoot(context, roots);
+        await ExtractFromRootAsync(context, roots);
+        _isSeeded = true;
     }
 
     private static void GetRootsElements(List<Root> roots, string filesPath)
@@ -64,321 +70,223 @@ public static class DbContextInitializer
         roots.AddRange(xmlFiles.Select(XmlHelper.DeserializeXmlFile<Root>));
     }
 
-    private static void ExtractFromRoot(DeathwatchDbContext context, List<Root> roots)
+    private static async Task ExtractFromRootAsync(DeathwatchDbContext context, List<Root> roots)
     {
         foreach (var root in roots.OfType<Root>())
         {
             if (!context.Accessoires.Any())
                 context.Accessoires.AddRange(root.AccessoiresArmes);
             else
-            {
-                foreach (var item in root.AccessoiresArmes.Where(item => !context.Accessoires.Any(e => e.Id == item.Id)))
-                {
-                    context.Accessoires.Add(item);
-                }
-            }
+                foreach (var item in
+                         root.AccessoiresArmes.Where(item => !context.Accessoires.Any(e => e.Id == item.Id)))
+                    await context.Accessoires.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Améliorations.Any())
                 context.Améliorations.AddRange(root.Améliorations);
             else
-            {
                 foreach (var item in root.Améliorations.Where(item => !context.Améliorations.Any(e => e.Id == item.Id)))
-                {
-                    context.Améliorations.Add(item);
-                }
-            }
+                    await context.Améliorations.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Archétypes.Any())
                 context.Archétypes.AddRange(root.Archétypes);
             else
-            {
                 foreach (var item in root.Archétypes.Where(item => !context.Archétypes.Any(e => e.Id == item.Id)))
-                {
-                    context.Archétypes.Add(item);
-                }
-            }
+                    await context.Archétypes.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Armes.Any())
                 context.Armes.AddRange(root.Armes);
             else
-            {
                 foreach (var item in root.Armes.Where(item => !context.Armes.Any(e => e.Id == item.Id)))
-                {
-                    context.Armes.Add(item);
-                }
-            }
+                    await context.Armes.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Armures.Any())
                 context.Armures.AddRange(root.Armures);
             else
-            {
                 foreach (var item in root.Armures.Where(item => !context.Armures.Any(e => e.Id == item.Id)))
-                {
-                    context.Armures.Add(item);
-                }
-            }
+                    await context.Armures.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Assermentations.Any())
                 context.Assermentations.AddRange(root.Assermentations);
             else
-            {
-                foreach (var item in root.Assermentations.Where(item => !context.Assermentations.Any(e => e.Id == item.Id)))
-                {
-                    context.Assermentations.Add(item);
-                }
-            }
+                foreach (var item in root.Assermentations.Where(item =>
+                             !context.Assermentations.Any(e => e.Id == item.Id)))
+                    await context.Assermentations.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Attributs.Any())
                 context.Attributs.AddRange(root.AttributsArmes);
             else
-            {
                 foreach (var item in root.AttributsArmes.Where(item => !context.Attributs.Any(e => e.Id == item.Id)))
-                {
-                    context.Attributs.Add(item);
-                }
-            }
+                    await context.Attributs.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Carrières.Any())
                 context.Carrières.AddRange(root.Carrières);
             else
-            {
                 foreach (var item in root.Carrières.Where(item => !context.Carrières.Any(e => e.Id == item.Id)))
-                {
-                    context.Carrières.Add(item);
-                }
-            }
+                    await context.Carrières.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Competences.Any())
                 context.Competences.AddRange(root.Competences);
             else
-            {
                 foreach (var item in root.Competences.Where(item => !context.Competences.Any(e => e.Id == item.Id)))
-                {
-                    context.Competences.Add(item);
-                }
-            }
+                    await context.Competences.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Coordonnées.Any())
                 context.Coordonnées.AddRange(root.Coordonnées);
             else
-            {
                 foreach (var item in root.Coordonnées.Where(item => !context.Coordonnées.Any(e => e.Id == item.Id)))
-                {
-                    context.Coordonnées.Add(item);
-                }
-            }
+                    await context.Coordonnées.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Cybernétiques.Any())
                 context.Cybernétiques.AddRange(root.Cybernétiques);
             else
-            {
                 foreach (var item in root.Cybernétiques.Where(item => !context.Cybernétiques.Any(e => e.Id == item.Id)))
-                {
-                    context.Cybernétiques.Add(item);
-                }
-            }
+                    await context.Cybernétiques.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Disciplines.Any())
                 context.Disciplines.AddRange(root.Disciplines);
             else
-            {
                 foreach (var item in root.Disciplines.Where(item => !context.Disciplines.Any(e => e.Id == item.Id)))
-                {
-                    context.Disciplines.Add(item);
-                }
-            }
+                    await context.Disciplines.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Divinations.Any())
                 context.Divinations.AddRange(root.Divinations);
             else
-            {
                 foreach (var item in root.Divinations.Where(item => !context.Divinations.Any(e => e.Id == item.Id)))
-                {
-                    context.Divinations.Add(item);
-                }
-            }
+                    await context.Divinations.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Equipements.Any())
                 context.Equipements.AddRange(root.Equipements);
             else
-            {
                 foreach (var item in root.Equipements.Where(item => !context.Equipements.Any(e => e.Id == item.Id)))
-                {
-                    context.Equipements.Add(item);
-                }
-            }
+                    await context.Equipements.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Implants.Any())
                 context.Implants.AddRange(root.Implants);
             else
-            {
                 foreach (var item in root.Implants.Where(item => !context.Implants.Any(e => e.Id == item.Id)))
-                {
-                    context.Implants.Add(item);
-                }
-            }
+                    await context.Implants.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Mondes.Any())
                 context.Mondes.AddRange(root.Mondes);
             else
-            {
                 foreach (var item in root.Mondes.Where(item => !context.Mondes.Any(e => e.Id == item.Id)))
-                {
-                    context.Mondes.Add(item);
-                }
-            }
+                    await context.Mondes.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Mutations.Any())
                 context.Mutations.AddRange(root.Mutations);
             else
-            {
                 foreach (var item in root.Mutations.Where(item => !context.Mutations.Any(e => e.Id == item.Id)))
-                {
-                    context.Mutations.Add(item);
-                }
-            }
+                    await context.Mutations.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Pouvoirs.Any())
                 context.Pouvoirs.AddRange(root.Pouvoirs);
             else
-            {
                 foreach (var item in root.Pouvoirs.Where(item => !context.Pouvoirs.Any(e => e.Id == item.Id)))
-                {
-                    context.Pouvoirs.Add(item);
-                }
-            }
+                    await context.Pouvoirs.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Promotions.Any())
                 context.Promotions.AddRange(root.Promotions);
             else
-            {
                 foreach (var item in root.Promotions.Where(item => !context.Promotions.Any(e => e.Id == item.Id)))
-                {
-                    context.Promotions.Add(item);
-                }
-            }
+                    await context.Promotions.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Seuils.Any())
                 context.Seuils.AddRange(root.SeuilsXp);
             else
-            {
                 foreach (var item in root.SeuilsXp.Where(item => !context.Seuils.Any(e => e.Id == item.Id)))
-                {
-                    context.Seuils.Add(item);
-                }
-            }
+                    await context.Seuils.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Sousmondes.Any())
                 context.Sousmondes.AddRange(root.Sousmondes);
             else
-            {
                 foreach (var item in root.Sousmondes.Where(item => !context.Sousmondes.Any(e => e.Id == item.Id)))
-                {
-                    context.Sousmondes.Add(item);
-                }
-            }
+                    await context.Sousmondes.AddAsync(item);
 
-            context.SaveChanges();
-
-            if (!context.Talents.Any())
-                context.Talents.AddRange(root.Talents);
-            else
-            {
-                foreach (var item in root.Talents.Where(item => !context.Talents.Any(e => e.Id == item.Id)))
-                {
-                    context.Talents.Add(item);
-                }
-            }
-
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Traits.Any())
                 context.Traits.AddRange(root.Traits);
             else
-            {
                 foreach (var item in root.Traits.Where(item => !context.Traits.Any(e => e.Id == item.Id)))
-                {
-                    context.Traits.Add(item);
-                }
-            }
+                    await context.Traits.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.Troubles.Any())
                 context.Troubles.AddRange(root.Troubles);
             else
-            {
                 foreach (var item in root.Troubles.Where(item => !context.Troubles.Any(e => e.Id == item.Id)))
-                {
-                    context.Troubles.Add(item);
-                }
-            }
+                    await context.Troubles.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.TypeMondes.Any())
                 context.TypeMondes.AddRange(root.TypeMondes);
             else
-            {
                 foreach (var item in root.TypeMondes.Where(item => !context.TypeMondes.Any(e => e.Id == item.Id)))
-                {
-                    context.TypeMondes.Add(item);
-                }
-            }
+                    await context.TypeMondes.AddAsync(item);
 
-            context.SaveChanges();
+            await context.SaveChangesAsync();
 
             if (!context.TypesNom.Any())
                 context.TypesNom.AddRange(root.TypesNom);
             else
-            {
                 foreach (var item in root.TypesNom.Where(item => !context.TypesNom.Any(e => e.Id == item.Id)))
+                    await context.TypesNom.AddAsync(item);
+
+            await context.SaveChangesAsync();
+
+            if (!context.Talents.Any())
+                context.Talents.AddRange(root.Talents);
+            else
+                foreach (var item in root.Talents)
                 {
-                    context.TypesNom.Add(item);
+                    if (!context.Talents.AsNoTracking().Any(e => e.Id == item.Id))
+                        await context.Talents.AddAsync(item);
+                    await context.SaveChangesAsync();
+
                 }
-            }
 
-            context.SaveChanges();
         }
     }
 }

+ 9 - 36
Dotnet/DataLayer/DeathwatchDbContext.cs

@@ -7,6 +7,15 @@ namespace DataLayer;
 
 public class DeathwatchDbContext : DbContext
 {
+
+    public DeathwatchDbContext()
+    {
+    }
+
+    public DeathwatchDbContext(DbContextOptions<DeathwatchDbContext> options) : base(options)
+    {
+    }
+
     public DbSet<Player> Players { get; set; }
     public DbSet<Weapon> Weapons { get; set; }
     public DbSet<Extension> Extensions { get; set; }
@@ -38,41 +47,6 @@ public class DeathwatchDbContext : DbContext
     public DbSet<Trouble> Troubles { get; set; }
     public DbSet<TypeMonde> TypeMondes { get; set; }
     public DbSet<TypeName> TypesNom { get; set; }
-    //public DbSet<AccessoiresArmes> AllAccessoiresArmes { get; set; }
-    //public DbSet<Améliorations> AllAméliorations { get; set; }
-    //public DbSet<Archétypes> AllArchétypes { get; set; }
-    //public DbSet<Armes> AllArmes { get; set; }
-    //public DbSet<Armures> AllArmures { get; set; }
-    //public DbSet<Assermentations> AllAssermentations { get; set; }
-    //public DbSet<AttributsArmes> AllAttributsArmes { get; set; }
-    //public DbSet<Carrières> AllCarrières { get; set; }
-    //public DbSet<Competences> AllCompetences { get; set; }
-    //public DbSet<Coordonnées> AllCoordonnées { get; set; }
-    //public DbSet<Cybernétiques> AllCybernétiques { get; set; }
-    //public DbSet<Disciplines> AllDisciplines { get; set; }
-    //public DbSet<Divinations> AllDivinations { get; set; }
-    //public DbSet<Equipements> AllEquipements { get; set; }
-    //public DbSet<Implants> AllImplants { get; set; }
-    //public DbSet<Mondes> AllMondes { get; set; }
-    //public DbSet<Mutations> AllMutations { get; set; }
-    //public DbSet<Pouvoirs> AllPouvoirs { get; set; }
-    //public DbSet<Promotions> AllPromotions { get; set; }
-    //public DbSet<SeuilsXp> AllSeuilsXp { get; set; }
-    //public DbSet<Sousmondes> AllSousmondes { get; set; }
-    //public DbSet<Talents> AllTalents { get; set; }
-    //public DbSet<Traits> AllTraits { get; set; }
-    //public DbSet<Troubles> AllTroubles { get; set; }
-    //public DbSet<TypeMondes> AllTypeMondes { get; set; }
-    //public DbSet<TypesNom> AllTypesNom { get; set; }
-
-
-    public DeathwatchDbContext() : base()
-    {
-    }
-
-    public DeathwatchDbContext(DbContextOptions<DeathwatchDbContext> options) : base(options)
-    {
-    }
 
     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
     {
@@ -258,6 +232,5 @@ public class DeathwatchDbContext : DbContext
         //    .HasMany(t => t.TypeNom)
         //    .WithOne()
         //    .HasForeignKey(x => x.TypeNomId);
-
     }
 }

+ 0 - 11
Dotnet/DataLayer/XMLTEST/01 - ldb.xml

@@ -1,11 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-
-<root>
-  <extensions>
-    <extension id="dh01" nom="livre de base"/>
-  </extensions>
-  <améliorations>
-    <amélioration id="01-amel2431" catégorie="technoprêtre" souscatégorie="magos explorator" promotion="8" type="compétence" nom="expression artistique (musicien)" cout="300" nb_max_ou_rang="1" />
-    <amélioration id="01-amel2432" catégorie="technoprêtre" souscatégorie="magos explorator" promotion="8" type="compétence" nom="marchandage" cout="300" nb_max_ou_rang="2" />
-  </améliorations>
-</root>

+ 1 - 1
Dotnet/Deathwatch/Deathwatch.csproj.user

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
-    <ActiveDebugProfile>Container (Dockerfile)</ActiveDebugProfile>
+    <ActiveDebugProfile>IIS Express</ActiveDebugProfile>
     <RazorPage_SelectedScaffolderID>RazorPageScaffolder</RazorPage_SelectedScaffolderID>
     <RazorPage_SelectedScaffolderCategoryPath>root/Common/RazorPage</RazorPage_SelectedScaffolderCategoryPath>
     <NameOfLastUsedPublishProfile>deathwatchweb - Web Deploy</NameOfLastUsedPublishProfile>

+ 6 - 3
Dotnet/Deathwatch/Program.cs

@@ -11,7 +11,10 @@ builder.Services.AddRazorPages();
 builder.Services.AddServerSideBlazor();
 
 builder.Services.AddDbContext<DeathwatchDbContext>(options =>
-    options.UseInMemoryDatabase("InMemoryDb"));
+{
+    options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
+    options.UseInMemoryDatabase("InMemoryDb");
+});
 
 builder.Services.AddTransient(typeof(IAbstractRepository<>), typeof(AbstractRepository<>));
 builder.Services.AddTransient<IPlayerService, PlayerService>();
@@ -30,9 +33,9 @@ using (var scope = scopeFactory.CreateScope())
     //var folderPath = Path.Combine($"{directory}", "Dotnet", "Deathwatch", "XML");
     var folderPath = Path.Combine($"{directory}", "XML");
     var db = scope.ServiceProvider.GetRequiredService<DeathwatchDbContext>();
-    if (db.Database.EnsureCreated())
+    if (await db.Database.EnsureCreatedAsync())
     {
-        DbContextInitializer.Initialize(db, folderPath);
+       await DbContextInitializer.Initialize(db, folderPath);
     }
 }