diff --git a/Sources/EF_UT/EntityTest.cs b/Sources/EF_UT/EntityTest.cs index d1f87b5..4122418 100644 --- a/Sources/EF_UT/EntityTest.cs +++ b/Sources/EF_UT/EntityTest.cs @@ -54,8 +54,8 @@ namespace EF_UT //prepares the database with one instance of the context using (var context = new LoLDbContext(options)) { - ChampionEntity chewie = new ChampionEntity("Chewbacca", "", ""); - ChampionEntity yoda = new ChampionEntity("Yoda", "", ""); + ChampionEntity chewie = new ChampionEntity("Chewbacca", "ewa", ""); + ChampionEntity yoda = new ChampionEntity("Yoda", "wewo", ""); ChampionEntity ewok = new ChampionEntity("Ewok", "", ""); context.Add(chewie); @@ -67,23 +67,23 @@ namespace EF_UT //prepares the database with one instance of the context using (var context = new LoLDbContext(options)) { - string NameToFind = "ew"; - Assert.AreEqual(2, context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).Count()); - NameToFind = "ewo"; - Assert.AreEqual(1, context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).Count()); - var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).First(); - ewok.Name = "Wicket"; + string BioToFind = "ew"; + Assert.AreEqual(2, context.Champions.Where(n => n.Bio.ToLower().Contains(BioToFind)).Count()); + BioToFind = "ewo"; + Assert.AreEqual(1, context.Champions.Where(n => n.Bio.ToLower().Contains(BioToFind)).Count()); + var ewok = context.Champions.Where(n => n.Bio.ToLower().Contains(BioToFind)).First(); + ewok.Bio = "Wicket"; context.SaveChanges(); } //prepares the database with one instance of the context - using (var context = new LoLDbContext(options)) - { - string NameToFind = "ew"; - Assert.AreEqual(1, context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).Count()); - NameToFind = "wick"; - Assert.AreEqual(1, context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).Count()); - } + //using (var context = new LoLDbContext(options)) + //{ + // string NameToFind = "ew"; + // Assert.AreEqual(1, context.Champions.Where(n => n.Bio.ToLower().Contains(NameToFind)).Count()); + // NameToFind = "wick"; + // Assert.AreEqual(1, context.Champions.Where(n => n.Bio.ToLower().Contains(NameToFind)).Count()); + //} } } } diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index 5d2c455..d672964 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -13,11 +13,11 @@ namespace EntityFramework [Table("Champion")] public class ChampionEntity { - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int Id { get; set; } + //[Key] + //[DatabaseGenerated(DatabaseGeneratedOption.Identity)] + //public int Id { get; set; } - [Required] + [Key] [MaxLength(50)] public string Name { get; set; } diff --git a/Sources/EntityFramework/EntityFramework.csproj b/Sources/EntityFramework/EntityFramework.csproj index 66899d0..2d1f7fc 100644 --- a/Sources/EntityFramework/EntityFramework.csproj +++ b/Sources/EntityFramework/EntityFramework.csproj @@ -17,4 +17,8 @@ + + + + diff --git a/Sources/EntityFramework/LoLDBContextWithStub.cs b/Sources/EntityFramework/LoLDBContextWithStub.cs new file mode 100644 index 0000000..7379162 --- /dev/null +++ b/Sources/EntityFramework/LoLDBContextWithStub.cs @@ -0,0 +1,25 @@ +using EntityFramework.Mapper; +using Microsoft.EntityFrameworkCore; +using StubLib; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramework +{ + public class LoLDBContextWithStub : LoLDbContext + { + protected override async void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + var stub = new StubData.ChampionsManager(new StubData()); + var list = await stub.GetItems(0, await stub.GetNbItems()); + modelBuilder.Entity().HasData( + list.Select(champion => champion.ToEntity()) + ); + } + } +} diff --git a/Sources/EntityFramework/LoLDbContext.cs b/Sources/EntityFramework/LoLDbContext.cs index 9fa1567..7d61851 100644 --- a/Sources/EntityFramework/LoLDbContext.cs +++ b/Sources/EntityFramework/LoLDbContext.cs @@ -29,11 +29,11 @@ namespace EntityFramework protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity().HasKey(entity => entity.Id); + modelBuilder.Entity().HasKey(entity => entity.Name); modelBuilder.Entity().ToTable("Champion"); - modelBuilder.Entity().Property(entity => entity.Id) - .ValueGeneratedOnAdd(); + //modelBuilder.Entity().Property(entity => entity.Id) + // .ValueGeneratedOnAdd(); modelBuilder.Entity().Property(entity => entity.Name) .IsRequired() diff --git a/Sources/EntityFramework/Mapper/ChampionMapper.cs b/Sources/EntityFramework/Mapper/ChampionMapper.cs new file mode 100644 index 0000000..862264d --- /dev/null +++ b/Sources/EntityFramework/Mapper/ChampionMapper.cs @@ -0,0 +1,16 @@ +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFramework.Mapper +{ + public static class ChampionMapper + { + public static ChampionEntity ToEntity(this Champion champion) { + return new ChampionEntity(champion.Name, champion.Bio, champion.Icon); + } + } +} diff --git a/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.Designer.cs b/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.Designer.cs deleted file mode 100644 index 94bb55c..0000000 --- a/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.Designer.cs +++ /dev/null @@ -1,85 +0,0 @@ -// -using System; -using EntityFramework; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace EntityFramework.Migrations -{ - [DbContext(typeof(LoLDbContext))] - [Migration("20230301152530_SkillMigration")] - partial class SkillMigration - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); - - modelBuilder.Entity("EntityFramework.ChampionEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Bio") - .IsRequired() - .HasMaxLength(500) - .HasColumnType("string") - .HasColumnName("Bio"); - - b.Property("Icon") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Champion", (string)null); - }); - - modelBuilder.Entity("EntityFramework.Skill", b => - { - b.Property("Name") - .HasColumnType("TEXT"); - - b.Property("ChampionEntityId") - .HasColumnType("INTEGER"); - - b.Property("Description") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Type") - .HasColumnType("INTEGER"); - - b.HasKey("Name"); - - b.HasIndex("ChampionEntityId"); - - b.ToTable("Skill"); - }); - - modelBuilder.Entity("EntityFramework.Skill", b => - { - b.HasOne("EntityFramework.ChampionEntity", null) - .WithMany("Skills") - .HasForeignKey("ChampionEntityId"); - }); - - modelBuilder.Entity("EntityFramework.ChampionEntity", b => - { - b.Navigation("Skills"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.cs b/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.cs deleted file mode 100644 index ce0154c..0000000 --- a/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace EntityFramework.Migrations -{ - /// - public partial class SkillMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Champion", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Name = table.Column(type: "TEXT", maxLength: 50, nullable: false), - Bio = table.Column(type: "string", maxLength: 500, nullable: false), - Icon = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Champion", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Skill", - columns: table => new - { - Name = table.Column(type: "TEXT", nullable: false), - Type = table.Column(type: "INTEGER", nullable: false), - Description = table.Column(type: "TEXT", nullable: false), - ChampionEntityId = table.Column(type: "INTEGER", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Skill", x => x.Name); - table.ForeignKey( - name: "FK_Skill_Champion_ChampionEntityId", - column: x => x.ChampionEntityId, - principalTable: "Champion", - principalColumn: "Id"); - }); - - migrationBuilder.CreateIndex( - name: "IX_Skill_ChampionEntityId", - table: "Skill", - column: "ChampionEntityId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Skill"); - - migrationBuilder.DropTable( - name: "Champion"); - } - } -} diff --git a/Sources/EntityFramework/Migrations/20230312170120_stubMig.Designer.cs b/Sources/EntityFramework/Migrations/20230312170120_stubMig.Designer.cs new file mode 100644 index 0000000..2b24874 --- /dev/null +++ b/Sources/EntityFramework/Migrations/20230312170120_stubMig.Designer.cs @@ -0,0 +1,83 @@ +// +using EntityFramework; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramework.Migrations +{ + [DbContext(typeof(LoLDBContextWithStub))] + [Migration("20230312170120_stubMig")] + partial class stubMig + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Property("Name") + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.Property("Bio") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("string") + .HasColumnName("Bio"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Champion", (string)null); + + b.HasData( + new + { + Name = "Akali", + Bio = "", + Icon = "" + }, + new + { + Name = "Aatrox", + Bio = "", + Icon = "" + }, + new + { + Name = "Ahri", + Bio = "", + Icon = "" + }, + new + { + Name = "Akshan", + Bio = "", + Icon = "" + }, + new + { + Name = "Bard", + Bio = "", + Icon = "" + }, + new + { + Name = "Alistar", + Bio = "", + Icon = "" + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramework/Migrations/20230312170120_stubMig.cs b/Sources/EntityFramework/Migrations/20230312170120_stubMig.cs new file mode 100644 index 0000000..3323fa4 --- /dev/null +++ b/Sources/EntityFramework/Migrations/20230312170120_stubMig.cs @@ -0,0 +1,49 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace EntityFramework.Migrations +{ + /// + public partial class stubMig : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Champion", + columns: table => new + { + Name = table.Column(type: "TEXT", maxLength: 50, nullable: false), + Bio = table.Column(type: "string", maxLength: 500, nullable: false), + Icon = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Champion", x => x.Name); + }); + + migrationBuilder.InsertData( + table: "Champion", + columns: new[] { "Name", "Bio", "Icon" }, + values: new object[,] + { + { "Aatrox", "", "" }, + { "Ahri", "", "" }, + { "Akali", "", "" }, + { "Akshan", "", "" }, + { "Alistar", "", "" }, + { "Bard", "", "" } + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Champion"); + } + } +} diff --git a/Sources/EntityFramework/Migrations/LoLDBContextWithStubModelSnapshot.cs b/Sources/EntityFramework/Migrations/LoLDBContextWithStubModelSnapshot.cs new file mode 100644 index 0000000..ba61c51 --- /dev/null +++ b/Sources/EntityFramework/Migrations/LoLDBContextWithStubModelSnapshot.cs @@ -0,0 +1,80 @@ +// +using EntityFramework; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramework.Migrations +{ + [DbContext(typeof(LoLDBContextWithStub))] + partial class LoLDBContextWithStubModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Property("Name") + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.Property("Bio") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("string") + .HasColumnName("Bio"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Champion", (string)null); + + b.HasData( + new + { + Name = "Akali", + Bio = "", + Icon = "" + }, + new + { + Name = "Aatrox", + Bio = "", + Icon = "" + }, + new + { + Name = "Ahri", + Bio = "", + Icon = "" + }, + new + { + Name = "Akshan", + Bio = "", + Icon = "" + }, + new + { + Name = "Bard", + Bio = "", + Icon = "" + }, + new + { + Name = "Alistar", + Bio = "", + Icon = "" + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs b/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs deleted file mode 100644 index 0abeee1..0000000 --- a/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs +++ /dev/null @@ -1,82 +0,0 @@ -// -using System; -using EntityFramework; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace EntityFramework.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("EntityFramework.ChampionEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Bio") - .IsRequired() - .HasMaxLength(500) - .HasColumnType("string") - .HasColumnName("Bio"); - - b.Property("Icon") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Name") - .IsRequired() - .HasMaxLength(50) - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("Champion", (string)null); - }); - - modelBuilder.Entity("EntityFramework.Skill", b => - { - b.Property("Name") - .HasColumnType("TEXT"); - - b.Property("ChampionEntityId") - .HasColumnType("INTEGER"); - - b.Property("Description") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Type") - .HasColumnType("INTEGER"); - - b.HasKey("Name"); - - b.HasIndex("ChampionEntityId"); - - b.ToTable("Skill"); - }); - - modelBuilder.Entity("EntityFramework.Skill", b => - { - b.HasOne("EntityFramework.ChampionEntity", null) - .WithMany("Skills") - .HasForeignKey("ChampionEntityId"); - }); - - modelBuilder.Entity("EntityFramework.ChampionEntity", b => - { - b.Navigation("Skills"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/Sources/EntityFramework/champion.db b/Sources/EntityFramework/champion.db index 662e972..b0ff12d 100644 Binary files a/Sources/EntityFramework/champion.db and b/Sources/EntityFramework/champion.db differ diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 5049af2..a5d27bb 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -18,6 +18,9 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{E39C3FBC-DE5E-4DAF-945A-98CE4ADE54D9}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFramework", "EntityFramework\EntityFramework.csproj", "{23483395-5091-4956-822F-17234E8C9E5C}" + ProjectSection(ProjectDependencies) = postProject + {2960F9BA-49DE-494D-92E3-CE5A794BA1A9} = {2960F9BA-49DE-494D-92E3-CE5A794BA1A9} + EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api_UT", "Api_UT\Api_UT.csproj", "{20A1A7DC-1E93-4506-BD32-8597A5DADD7B}" EndProject