diff --git a/Sources/Client/Program.cs b/Sources/Client/Program.cs index b799e30..5c8e5dc 100644 --- a/Sources/Client/Program.cs +++ b/Sources/Client/Program.cs @@ -8,19 +8,22 @@ using EntityFramwork.Factories; using Model; using StubLib; -/* + StubData tmp = new StubData(); var tmpListe = await tmp.ChampionsMgr.GetItemsByName("Akali", 0, 6); Champion champ = tmpListe.First(); Factories facto = new Factories(); -EntityChampions entity = facto.ModelToEntity(champ); +EntityChampions entity = facto.ChampionModelToEntity(champ); +List skins = new List(entity.Skins); + -using ( BDDContext context = new BDDContext()) +using ( BDDContext db = new BDDContext()) { - context.Add(entity); - context.SaveChanges(); + db.Add(entity); + db.SaveChanges(); } -*/ + +/* Console.WriteLine("Start"); Console.ReadLine(); @@ -40,4 +43,5 @@ DtoChampions champ2 = await client.GetChampion("toto"); Console.WriteLine(champ2.name); Console.WriteLine("End"); -Console.ReadLine(); \ No newline at end of file +Console.ReadLine(); +*/ \ No newline at end of file diff --git a/Sources/EntityFramwork/BDD-APILOL.db b/Sources/EntityFramwork/BDD-APILOL.db index 01f0722..3121d98 100644 Binary files a/Sources/EntityFramwork/BDD-APILOL.db and b/Sources/EntityFramwork/BDD-APILOL.db differ diff --git a/Sources/EntityFramwork/BDD-APILOL.db-shm b/Sources/EntityFramwork/BDD-APILOL.db-shm new file mode 100644 index 0000000..2e0d47f Binary files /dev/null and b/Sources/EntityFramwork/BDD-APILOL.db-shm differ diff --git a/Sources/EntityFramwork/BDD-APILOL.db-wal b/Sources/EntityFramwork/BDD-APILOL.db-wal new file mode 100644 index 0000000..cf1dcae Binary files /dev/null and b/Sources/EntityFramwork/BDD-APILOL.db-wal differ diff --git a/Sources/EntityFramwork/BDDContext.cs b/Sources/EntityFramwork/BDDContext.cs index 7f054ac..fe549b6 100644 --- a/Sources/EntityFramwork/BDDContext.cs +++ b/Sources/EntityFramwork/BDDContext.cs @@ -8,6 +8,27 @@ namespace EntityFramwork public BDDContext() { } public BDDContext(DbContextOptions option) : base(option) { } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + //création de la table Champion + modelBuilder.Entity().HasKey(a => a.Id); + modelBuilder.Entity().Property(a => a.Id) + .ValueGeneratedOnAdd(); + + //création de la table Skins + modelBuilder.Entity().HasKey(m => m.Id); + modelBuilder.Entity().Property(m => m.Id) + .ValueGeneratedOnAdd(); + + //modelBuilder.Entity().Property("ChampionForeignKey"); + // Use the shadow property as a foreign key + modelBuilder.Entity() + .HasOne(m => m.Champion) + .WithMany(a => a.Skins) + .HasForeignKey("ChampionsForeignKey"); + } public DbSet Champions { get; set; } public DbSet Skins { get; set; } diff --git a/Sources/EntityFramwork/EntityChampions.cs b/Sources/EntityFramwork/EntityChampions.cs index 4708116..8b878ad 100644 --- a/Sources/EntityFramwork/EntityChampions.cs +++ b/Sources/EntityFramwork/EntityChampions.cs @@ -1,4 +1,5 @@ -using Model; +using EntityFramwork; +using Model; using System; using System.Collections.Generic; using System.Linq; @@ -9,7 +10,7 @@ namespace DTO { public class EntityChampions { - public long Id { get; set; } + public int Id { get; set; } public string Name { get; set; } @@ -17,7 +18,7 @@ namespace DTO public string Icon { get; set; } - //public List Skins { get; set; } + public List Skins { get; set; } } } diff --git a/Sources/EntityFramwork/EntitySkins.cs b/Sources/EntityFramwork/EntitySkins.cs index 17ac2a4..c956415 100644 --- a/Sources/EntityFramwork/EntitySkins.cs +++ b/Sources/EntityFramwork/EntitySkins.cs @@ -11,16 +11,14 @@ namespace EntityFramwork { public class EntitySkins { - public long Id { get; set; } + public int Id { get; set; } public string Description { get; set; } public string Icon { get; set; } public float Price { get; set; } + + public EntityChampions Champion { get; set; } - [ForeignKey("ChampionsForeignKey")] - public EntityChampions EntityChampion { get; set; } - - public long ChampionsForeignKey { get;set; } } } diff --git a/Sources/EntityFramwork/Factories/Factories.cs b/Sources/EntityFramwork/Factories/Factories.cs index 3c8bdf3..dc491c6 100644 --- a/Sources/EntityFramwork/Factories/Factories.cs +++ b/Sources/EntityFramwork/Factories/Factories.cs @@ -5,14 +5,29 @@ namespace EntityFramwork.Factories { public class Factories { - public EntityChampions ModelToEntity(Champion champ) + public EntityChampions ChampionModelToEntity(Champion champ,int sansSkin = 0) { EntityChampions entity = new EntityChampions(); entity.Name = champ.Name; entity.Bio = champ.Bio; entity.Icon = champ.Icon; - //entity.Skins = new List(champ.Skins); + if ( sansSkin == 0) + { + entity.Skins = champ.Skins.Select(Model => this.SkinsModelToEntity(Model)).ToList(); + } + + return entity; + } + + public EntitySkins SkinsModelToEntity(Skin skin) + { + EntitySkins entity= new EntitySkins(); + + entity.Price = skin.Price; + entity.Icon = skin.Icon; + entity.Description= skin.Description; + entity.Champion = ChampionModelToEntity(skin.Champion,1); return entity; } diff --git a/Sources/EntityFramwork/Migrations/20230201131800_migrationTest.cs b/Sources/EntityFramwork/Migrations/20230201131800_migrationTest.cs deleted file mode 100644 index 73050ce..0000000 --- a/Sources/EntityFramwork/Migrations/20230201131800_migrationTest.cs +++ /dev/null @@ -1,54 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace EntityFramwork.Migrations -{ - /// - public partial class migrationTest : 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", nullable: false), - Bio = table.Column(type: "TEXT", nullable: false), - Icon = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Champions", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Skins", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Description = table.Column(type: "TEXT", nullable: false), - Icon = table.Column(type: "TEXT", nullable: false), - Price = table.Column(type: "REAL", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Skins", x => x.Id); - }); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Champions"); - - migrationBuilder.DropTable( - name: "Skins"); - } - } -} diff --git a/Sources/EntityFramwork/Migrations/20230201131800_migrationTest.Designer.cs b/Sources/EntityFramwork/Migrations/20230204085418_TEST.Designer.cs similarity index 68% rename from Sources/EntityFramwork/Migrations/20230201131800_migrationTest.Designer.cs rename to Sources/EntityFramwork/Migrations/20230204085418_TEST.Designer.cs index 364cbd8..ec97c73 100644 --- a/Sources/EntityFramwork/Migrations/20230201131800_migrationTest.Designer.cs +++ b/Sources/EntityFramwork/Migrations/20230204085418_TEST.Designer.cs @@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace EntityFramwork.Migrations { [DbContext(typeof(BDDContext))] - [Migration("20230201131800_migrationTest")] - partial class migrationTest + [Migration("20230204085418_TEST")] + partial class TEST { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -21,7 +21,7 @@ namespace EntityFramwork.Migrations modelBuilder.Entity("DTO.EntityChampions", b => { - b.Property("Id") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); @@ -44,10 +44,13 @@ namespace EntityFramwork.Migrations modelBuilder.Entity("EntityFramwork.EntitySkins", b => { - b.Property("Id") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("ChampionsForeignKey") + .HasColumnType("INTEGER"); + b.Property("Description") .IsRequired() .HasColumnType("TEXT"); @@ -61,8 +64,26 @@ namespace EntityFramwork.Migrations b.HasKey("Id"); + b.HasIndex("ChampionsForeignKey"); + b.ToTable("Skins"); }); + + modelBuilder.Entity("EntityFramwork.EntitySkins", b => + { + b.HasOne("DTO.EntityChampions", "Champion") + .WithMany("Skins") + .HasForeignKey("ChampionsForeignKey") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Champion"); + }); + + modelBuilder.Entity("DTO.EntityChampions", b => + { + b.Navigation("Skins"); + }); #pragma warning restore 612, 618 } } diff --git a/Sources/EntityFramwork/Migrations/20230204085418_TEST.cs b/Sources/EntityFramwork/Migrations/20230204085418_TEST.cs new file mode 100644 index 0000000..148e9bf --- /dev/null +++ b/Sources/EntityFramwork/Migrations/20230204085418_TEST.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFramwork.Migrations +{ + /// + public partial class TEST : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs b/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs index 832c08b..dc6ff1d 100644 --- a/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs +++ b/Sources/EntityFramwork/Migrations/BDDContextModelSnapshot.cs @@ -18,7 +18,7 @@ namespace EntityFramwork.Migrations modelBuilder.Entity("DTO.EntityChampions", b => { - b.Property("Id") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); @@ -41,10 +41,13 @@ namespace EntityFramwork.Migrations modelBuilder.Entity("EntityFramwork.EntitySkins", b => { - b.Property("Id") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); + b.Property("ChampionsForeignKey") + .HasColumnType("INTEGER"); + b.Property("Description") .IsRequired() .HasColumnType("TEXT"); @@ -58,8 +61,26 @@ namespace EntityFramwork.Migrations b.HasKey("Id"); + b.HasIndex("ChampionsForeignKey"); + b.ToTable("Skins"); }); + + modelBuilder.Entity("EntityFramwork.EntitySkins", b => + { + b.HasOne("DTO.EntityChampions", "Champion") + .WithMany("Skins") + .HasForeignKey("ChampionsForeignKey") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Champion"); + }); + + modelBuilder.Entity("DTO.EntityChampions", b => + { + b.Navigation("Skins"); + }); #pragma warning restore 612, 618 } }