diff --git a/.vs/API/v17/.suo b/.vs/API/v17/.suo index 76b671d..064ca0b 100644 Binary files a/.vs/API/v17/.suo and b/.vs/API/v17/.suo differ diff --git a/README.md b/README.md index 06487b4..53081bc 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,8 @@ Et enfin cliquer sur l'icône ci-dessous pour faire la connexion à la solution -Vous pouvez dorénavant voir toutes les tables qui y sont enregistrées ! +Vous pouvez dorénavant voir toutes les tables qui y sont enregistrées ! Si vous souhaitez ajouter des modifications à la base de données et les visualiser, +réaliser à nouveau la migration (ou *updater* celui actuel), puis supprimer toutes les tables et lancer la commande : **dotnet ef database update** et enfin rafraichiser la BD ! :information_source: *Notez qu'il est également possible grâce à SQLLite d'ajouter, modifier ou supprimer des données dans les tables.* diff --git a/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db b/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db index d146bd7..2553478 100644 Binary files a/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db and b/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db differ diff --git a/src/EntityFramework_LoL/Sources/MyFlib/DataSeeder.cs b/src/EntityFramework_LoL/Sources/MyFlib/DataSeeder.cs new file mode 100644 index 0000000..eeb743a --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/DataSeeder.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyFlib +{ + public static class DataSeeder + { + public static void SeedData(LolDbContext context) + { + ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" }; + ChampionEntity nasus = new ChampionEntity { Name = "Nasus", Class = ChampionClassEntity.Tank, Bio = "", Icon = "", Image = "" }; + ChampionEntity ashe = new ChampionEntity { Name = "Ashe", Class = ChampionClassEntity.Marksman, Bio = "", Icon = "", Image = "" }; + + context.AddRange(hecarim, nasus, ashe); + + context.SaveChanges(); + } + } +} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Entities/ChampionEntity.cs b/src/EntityFramework_LoL/Sources/MyFlib/Entities/ChampionEntity.cs index 98313cf..dbd29c1 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/Entities/ChampionEntity.cs +++ b/src/EntityFramework_LoL/Sources/MyFlib/Entities/ChampionEntity.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -10,14 +11,13 @@ namespace MyFlib public class ChampionEntity { [Key] - public int Id { get; set; } + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public Guid Id { get; set; } [MaxLength(255)] public string Name { get; set; } [MaxLength(255)] public string Bio { get; set; } - [MaxLength(255)] public string Icon { get; set; } - [MaxLength(255)] public string Image { get; set; } public ChampionClassEntity Class { get; set; } diff --git a/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs b/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs index 8b78047..5ff7132 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs +++ b/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs @@ -28,12 +28,12 @@ namespace MyFlib modelBuilder.Entity().HasKey(e => e.Id); modelBuilder.Entity().Property(e => e.Id).ValueGeneratedOnAdd(); modelBuilder.Entity().HasData( - new ChampionEntity { Id = 1, Name = "Akali", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" }, - new ChampionEntity { Id = 2, Name = "Aatrox", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" }, - new ChampionEntity { Id = 3, Name = "Ahri", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" }, - new ChampionEntity { Id = 4, Name = "Akshan", Class = ChampionClassEntity.Marksman, Bio = "", Icon = "", Image = "" }, - new ChampionEntity { Id = 5, Name = "Bard", Class = ChampionClassEntity.Support, Bio = "", Icon = "", Image = "" }, - new ChampionEntity { Id = 6, Name = "Alistar", Class = ChampionClassEntity.Tank, Bio = "", Icon = "", Image = "" } + new ChampionEntity { Id = Guid.Parse("{4422C524-B2CB-43EF-8263-990C3CEA7CAE}"), Name = "Akali", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" }, + new ChampionEntity { Id = Guid.Parse("{A4F84D92-C20F-4F2D-B3F9-CA00EF556E72}"), Name = "Aatrox", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" }, + new ChampionEntity { Id = Guid.Parse("{AE5FE535-F041-445E-B570-28B75BC78CB9}"), Name = "Ahri", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" }, + new ChampionEntity { Id = Guid.Parse("{3708dcfd-02a1-491e-b4f7-e75bf274cf23}"), Name = "Akshan", Class = ChampionClassEntity.Marksman, Bio = "", Icon = "", Image = "" }, + new ChampionEntity { Id = Guid.Parse("{7f7746fa-b1cb-49da-9409-4b3e6910500e}"), Name = "Bard", Class = ChampionClassEntity.Support, Bio = "", Icon = "", Image = "" }, + new ChampionEntity { Id = Guid.Parse("{36ad2a82-d17b-47de-8a95-6e154a7df557}"), Name = "Alistar", Class = ChampionClassEntity.Tank, Bio = "", Icon = "", Image = "" } ); } diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204131532_MyMigration.Designer.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204131532_MyMigration.Designer.cs deleted file mode 100644 index 8cfaffe..0000000 --- a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204131532_MyMigration.Designer.cs +++ /dev/null @@ -1,114 +0,0 @@ -// -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using MyFlib; - -#nullable disable - -namespace MyFlib.Migrations -{ - [DbContext(typeof(LolDbContext))] - [Migration("20230204131532_MyMigration")] - partial class MyMigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); - - modelBuilder.Entity("MyFlib.ChampionEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Bio") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.Property("Class") - .HasColumnType("INTEGER"); - - b.Property("Icon") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.Property("Image") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Champions"); - - b.HasData( - new - { - Id = 1, - Bio = "", - Class = 1, - Icon = "", - Image = "", - Name = "Akali" - }, - new - { - Id = 2, - Bio = "", - Class = 2, - Icon = "", - Image = "", - Name = "Aatrox" - }, - new - { - Id = 3, - Bio = "", - Class = 3, - Icon = "", - Image = "", - Name = "Ahri" - }, - new - { - Id = 4, - Bio = "", - Class = 4, - Icon = "", - Image = "", - Name = "Akshan" - }, - new - { - Id = 5, - Bio = "", - Class = 5, - Icon = "", - Image = "", - Name = "Bard" - }, - new - { - Id = 6, - Bio = "", - Class = 6, - Icon = "", - Image = "", - Name = "Alistar" - }); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204131532_MyMigration.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204131532_MyMigration.cs deleted file mode 100644 index 2480c4e..0000000 --- a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204131532_MyMigration.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional - -namespace MyFlib.Migrations -{ - /// - public partial class MyMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Champions", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Name = table.Column(type: "TEXT", maxLength: 255, nullable: false), - Bio = table.Column(type: "TEXT", maxLength: 255, nullable: false), - Icon = table.Column(type: "TEXT", maxLength: 255, nullable: false), - Image = table.Column(type: "TEXT", maxLength: 255, nullable: false), - Class = table.Column(type: "INTEGER", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Champions", x => x.Id); - }); - - migrationBuilder.InsertData( - table: "Champions", - columns: new[] { "Id", "Bio", "Class", "Icon", "Image", "Name" }, - values: new object[,] - { - { 1, "", 1, "", "", "Akali" }, - { 2, "", 2, "", "", "Aatrox" }, - { 3, "", 3, "", "", "Ahri" }, - { 4, "", 4, "", "", "Akshan" }, - { 5, "", 5, "", "", "Bard" }, - { 6, "", 6, "", "", "Alistar" } - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Champions"); - } - } -} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/LolDbContextModelSnapshot.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/LolDbContextModelSnapshot.cs deleted file mode 100644 index 76cb456..0000000 --- a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/LolDbContextModelSnapshot.cs +++ /dev/null @@ -1,111 +0,0 @@ -// -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using MyFlib; - -#nullable disable - -namespace MyFlib.Migrations -{ - [DbContext(typeof(LolDbContext))] - partial class LolDbContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); - - modelBuilder.Entity("MyFlib.ChampionEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Bio") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.Property("Class") - .HasColumnType("INTEGER"); - - b.Property("Icon") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.Property("Image") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(255) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Champions"); - - b.HasData( - new - { - Id = 1, - Bio = "", - Class = 1, - Icon = "", - Image = "", - Name = "Akali" - }, - new - { - Id = 2, - Bio = "", - Class = 2, - Icon = "", - Image = "", - Name = "Aatrox" - }, - new - { - Id = 3, - Bio = "", - Class = 3, - Icon = "", - Image = "", - Name = "Ahri" - }, - new - { - Id = 4, - Bio = "", - Class = 4, - Icon = "", - Image = "", - Name = "Akshan" - }, - new - { - Id = 5, - Bio = "", - Class = 5, - Icon = "", - Image = "", - Name = "Bard" - }, - new - { - Id = 6, - Bio = "", - Class = 6, - Icon = "", - Image = "", - Name = "Alistar" - }); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Program.cs b/src/EntityFramework_LoL/Sources/MyFlib/Program.cs index 3e22f35..0da5a3c 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/Program.cs +++ b/src/EntityFramework_LoL/Sources/MyFlib/Program.cs @@ -6,7 +6,16 @@ using (var context = new LolDbContext()) foreach (var c in context.Champions) { - Console.WriteLine($"{c.Name} - {c.Bio}"); + Console.WriteLine($"{c.Name} - {c.Class}"); } + + Console.WriteLine("\nWith new Champions :\n"); + + DataSeeder.SeedData(context); + foreach (var c in context.Champions) + { + Console.WriteLine($"{c.Name} - {c.Class}"); + } + context.SaveChangesAsync(); // or context.SaveChangesAsync } \ No newline at end of file diff --git a/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs b/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs index 1f99d6a..fd66059 100644 --- a/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs +++ b/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs @@ -1,4 +1,3 @@ - using Microsoft.EntityFrameworkCore; using MyFlib; using Xunit; @@ -20,9 +19,7 @@ namespace UT_EF ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" }; ChampionEntity yuumi = new ChampionEntity { Name = "yuumi", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" }; - context.Champions.Add(sylas); - context.Champions.Add(hecarim); - context.Champions.Add(yuumi); + context.Champions.AddRange(sylas, hecarim, yuumi); context.SaveChanges(); } @@ -46,20 +43,22 @@ namespace UT_EF ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" }; ChampionEntity yuumi = new ChampionEntity { Name = "yuumi", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" }; - context.Champions.Add(sylas); - context.Champions.Add(hecarim); - context.Champions.Add(yuumi); + context.Champions.AddRange(sylas, hecarim, yuumi); context.SaveChanges(); } using (var context = new LolDbContext(options)) { string nameToFind = "m"; + ChampionClassEntity type = ChampionClassEntity.Fighter; Assert.Equal(2, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); + Assert.Equal(1, context.Champions.Where(c => c.Class == type).Count()); nameToFind = "yuu"; Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); var ewok = context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).First(); ewok.Name = "Garen"; + ewok.Bio = "Magic resist"; + ewok.Class = type; context.SaveChanges(); } @@ -68,8 +67,50 @@ namespace UT_EF string nameToFind = "m"; Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); nameToFind = "garen"; + string bioToFind = "magic resist"; + ChampionClassEntity type = ChampionClassEntity.Fighter; + Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); + Assert.Equal(1, context.Champions.Where(c => c.Bio.ToLower().Contains(bioToFind)).Count()); + Assert.Equal(2, context.Champions.Where(c => c.Class == type).Count()); + } + } + + [Fact] + public void Delete_Test() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Delete_Test_database") + .Options; + + using (var context = new LolDbContext(options)) + { + ChampionEntity sylas = new ChampionEntity { Name = "Sylas", Class = ChampionClassEntity.Assassin, Bio = "", Icon = "", Image = "" }; + ChampionEntity hecarim = new ChampionEntity { Name = "Hecarim", Class = ChampionClassEntity.Fighter, Bio = "", Icon = "", Image = "" }; + ChampionEntity yuumi = new ChampionEntity { Name = "yuumi", Class = ChampionClassEntity.Mage, Bio = "", Icon = "", Image = "" }; + + context.Champions.AddRange(sylas, hecarim, yuumi); + context.SaveChanges(); + } + + using (var context = new LolDbContext(options)) + { + var ewok = context.Champions.First(); + string nameToFind = "sylas"; Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); + Assert.Equal(3, context.Champions.Count()); + context.Champions.Remove(ewok); + context.SaveChanges(); + } + + using (var context = new LolDbContext(options)) + { + string nameToFind = "sylas"; + Assert.Equal(0, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); + Assert.Equal(2, context.Champions.Count()); + + } + } } } \ No newline at end of file