From 6c2cd1d33923ee92ff31c3b2c511e96be8b1b281 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Wed, 1 Feb 2023 17:45:11 +0100 Subject: [PATCH 1/2] Creating DataBase, Entity Champion and DBContext :white_check_mark: --- .vs/LolProject/v17/.suo | Bin 13824 -> 12800 bytes .../Sources/LeagueOfLegends.sln | 8 +++- .../Sources/MyFlib/ChampionEntity.cs | 19 +++++++++ .../Sources/MyFlib/DataBase.db | Bin 0 -> 20480 bytes .../Sources/MyFlib/LolDbContext.cs | 26 ++++++++++++ .../20230201163620_MyMigration.Designer.cs | 40 ++++++++++++++++++ .../Migrations/20230201163620_MyMigration.cs | 37 ++++++++++++++++ .../Migrations/LolDbContextModelSnapshot.cs | 37 ++++++++++++++++ .../Sources/MyFlib/MyFlib.csproj | 25 +++++++++++ .../Sources/MyFlib/Program.cs | 10 +++++ 10 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/ChampionEntity.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/DataBase.db create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Migrations/LolDbContextModelSnapshot.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/MyFlib.csproj create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Program.cs diff --git a/.vs/LolProject/v17/.suo b/.vs/LolProject/v17/.suo index d3190eff2d84debdc910ff8157ca8d8816dcad71..d173a870bd5da057848024bbf125a2747188c1ef 100644 GIT binary patch delta 1117 zcmb_a&ubGw6yDiPOw!KghlxqsY?>NV1W6>xCaD3H#(G#tBzy1=Xibbgc<``7kMZE$ z=AAqV-n>co9}rRy6ctY*UIfpALJrAAeQ!5YS|Os~gU8Ig`QCi*ee(u)2fgQQmBrw! znhkw^eC^JS`bU<8`|4X5>1B9P;jrgx3$7@NAN?m9yieq{6+E0mCkp`M>GcKN{h)ia zDDGqEVe}xnhE8z-O`c*YXNNyw``f{x+MY3Qf4lmZ%rFk*o5V%$k)6hU2t6{3oxzp( zWG#fDI%p-M_)!H13nmXzHr-HTV23TCVn9CU2FKx!rqII#BlDmUOHg-Ifea1}9<1S+ zY#28G_uDXuzlM=ohLXyiNutFxM$8#xlNErS?6{E4H4c+xOJf1NT{?kCS%M^@Z<1+# z(li0nNDB`264+5oEMi0|?a2_m31-2BN3nYcOCgr;-fC<#`a89vVchN)tKD9y*ytMl zV!v12D3@#XzR{~earp~Gb5%h^`Y}SVa~+KdjL6^dIC7Rq&0w-Nu+OfRAP5cTQSN|b(A#jRVpo}WuvNGMgVimK#SxV_jvTLAB0USp^kI!V zds&l37au&_ef4zr@+T$z;my})YtH_K5*w~u7e37RO@GUk@DKW@=j)$+Iftq+(irLY zk7_y7^%iqgk*#^Nd)^W>6DKqrXFBucamE)rOjD>``ZOsJ*a>gk+=(p38E@7vRh&rW GyYCl!E}laG delta 1285 zcmb_c&ubG=5YF3d+cbTfrcK%;P5MjMpLCmSx@orPA%(V96X^3$a`G6pcnWy% zq6ae<{|wu`dI(fd5G*}pR7u zjo?0vYY>-&i+mwi7Guxv1_B9m4B^_o@q*Qb5=%Mtzcdd2vOY8i}ICyW4Isg zo@4lwI@QEzsOuzX-pV_{bumCN=xu5Xsv(0DU~-T-&Sq#sVo~&t_k<1`4lPv44Xvj| zmV?$saj-AZbBT4c_Y ztEUX!hc_;H4z|LK2*=T)aR)9Wm3?{7ai z^4*%dGjv^ea=25o_vJK$c4CzinW>&C6Wx3h1`pr`eLikJs3uWkbRrSE-aZNup*8*H zzSmpu_G70?=9cqaJG}3VhM>6m>q1R_>;j527?QPzEm@a=af}exW z9(9O0keBOccRRHHGYym1em^C{msNBCqso>Q(++go*0ff^HAbANqd)Sy=OisMddU0c{5q1sWitUAH!qn8ang z2v5MvaOT8Ij3aKxvVu}^M3uglt=K=sj(;EJ;^gpoCr%>ub~>AciE4;DLQ3&SDItWR z+m>!OWiig*4D?m5^w)I_Q4Q{v%%37(-V^0-=GXFG`OCVSi6ejj0tg_000IagfB*sr z{6~RLxq?-x$jhfm_<9_*-iMRZczQBVJNeeobA3@;qx`=z4N#qNcZ$X330 zMiayTI#`>%p zgy+e0Ili41)YBEVE{|lvYBuHP{&L4b;63Ta?`C1L*!Ht{o=j&KnL*>vOfv7e%_?29 zJ^Dv-YZh8b^xmO0mQYEGl()av#9#=cVvx)7@0R#|0009ILKmY**5I|sK1@7hC-Sr;OV literal 0 HcmV?d00001 diff --git a/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs b/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs new file mode 100644 index 0000000..71e5157 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/LolDbContext.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyFlib +{ + public class LolDbContext: DbContext + { + public DbSet Champions { get; set; } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + optionsBuilder.UseSqlite(@"Data Source=DataBase.db"); + } + } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasData(new ChampionEntity("Akali")); + } + + } +} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs new file mode 100644 index 0000000..6ce17ec --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs @@ -0,0 +1,40 @@ +// +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("20230201163620_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("Name") + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Champions"); + + b.HasData( + new + { + Name = "Akali" + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs new file mode 100644 index 0000000..8716d00 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs @@ -0,0 +1,37 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MyFlib.Migrations +{ + /// + public partial class MyMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Champions", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Champions", x => x.Name); + }); + + migrationBuilder.InsertData( + table: "Champions", + column: "Name", + value: "Akali"); + } + + /// + 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 new file mode 100644 index 0000000..5f71390 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/LolDbContextModelSnapshot.cs @@ -0,0 +1,37 @@ +// +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("Name") + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Champions"); + + b.HasData( + new + { + Name = "Akali" + }); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/MyFlib.csproj b/src/EntityFramework_LoL/Sources/MyFlib/MyFlib.csproj new file mode 100644 index 0000000..f99623c --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/MyFlib.csproj @@ -0,0 +1,25 @@ + + + + Exe + net6.0 + enable + enable + $(MSBuildProjectDirectory) + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Program.cs b/src/EntityFramework_LoL/Sources/MyFlib/Program.cs new file mode 100644 index 0000000..ef4ef6d --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/Program.cs @@ -0,0 +1,10 @@ +// See https://aka.ms/new-console-template for more information +using MyFlib; + +Console.WriteLine("Hello, World!"); + +using(var context = new LolDbContext()) +{ + //var champions = context.Champions.where + context.SaveChangesAsync(); // or context.SaveChangesAsync +} \ No newline at end of file From c0c61816ff7596267707faa1f8844df9331c7f61 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Sat, 4 Feb 2023 11:30:08 +0100 Subject: [PATCH 2/2] Update Entity Champion and create UT for Add and Update Champion --- .../Sources/ApiLol/ApiLol.csproj | 1 + .../ApiLol/Controllers/ChampionsController.cs | 4 +- .../Sources/DTO/DTO.csproj | 4 + .../Sources/LeagueOfLegends.sln | 9 +- .../Sources/Model/Model.csproj | 3 + .../Sources/MyFlib/ChampionEntity.cs | 19 --- .../Sources/MyFlib/DataBase.db | Bin 20480 -> 20480 bytes .../Sources/MyFlib/Entities/ChampionEntity.cs | 25 ++++ .../Sources/MyFlib/LolDbContext.cs | 22 +++- .../20230201163620_MyMigration.Designer.cs | 40 ------ .../Migrations/20230201163620_MyMigration.cs | 37 ------ .../20230204102522_MyMigration.Designer.cs | 114 ++++++++++++++++++ .../Migrations/20230204102522_MyMigration.cs | 53 ++++++++ .../Migrations/LolDbContextModelSnapshot.cs | 76 +++++++++++- .../Sources/MyFlib/MyFlib.csproj | 1 + .../Sources/MyFlib/Program.cs | 10 +- .../MyFlib/enums/ChampionClassEntity.cs | 19 +++ .../Sources/Shared/Shared.csproj | 4 + .../Sources/StubLib/StubLib.csproj | 4 + .../Tests/ConsoleTests/ConsoleTests.csproj | 1 + .../Sources/Tests/UT_EF/ChampionsTest.cs | 75 ++++++++++++ .../Sources/Tests/UT_EF/UT_EF.csproj | 29 +++++ .../Sources/Tests/UT_EF/Usings.cs | 1 + 23 files changed, 444 insertions(+), 107 deletions(-) delete mode 100644 src/EntityFramework_LoL/Sources/MyFlib/ChampionEntity.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Entities/ChampionEntity.cs delete mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs delete mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204102522_MyMigration.Designer.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204102522_MyMigration.cs create mode 100644 src/EntityFramework_LoL/Sources/MyFlib/enums/ChampionClassEntity.cs create mode 100644 src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs create mode 100644 src/EntityFramework_LoL/Sources/Tests/UT_EF/UT_EF.csproj create mode 100644 src/EntityFramework_LoL/Sources/Tests/UT_EF/Usings.cs diff --git a/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj b/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj index e9239f1..53e03ba 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj +++ b/src/EntityFramework_LoL/Sources/ApiLol/ApiLol.csproj @@ -7,6 +7,7 @@ + diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionsController.cs index c19a41a..d06fbaa 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/ChampionsController.cs @@ -17,8 +17,8 @@ namespace ApiLol.Controllers [HttpGet] public async Task Get() { - var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); // Le await va permettre que les lignes suivantes ne s'éxécute pas - return Ok(new { result = champions.Select(c => c.ToDto())}); + var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); // Le await va permettre que les lignes suivantes ne s'éxécute pas + return Ok(new { result = champions.Select(c => c.ToDto()) }); } // GET api//5 diff --git a/src/EntityFramework_LoL/Sources/DTO/DTO.csproj b/src/EntityFramework_LoL/Sources/DTO/DTO.csproj index bafd05b..9c33615 100644 --- a/src/EntityFramework_LoL/Sources/DTO/DTO.csproj +++ b/src/EntityFramework_LoL/Sources/DTO/DTO.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln b/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln index a1cac0d..558a0e2 100644 --- a/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln +++ b/src/EntityFramework_LoL/Sources/LeagueOfLegends.sln @@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiLol", "ApiLol\ApiLol.csp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{3919E408-EB12-4422-989B-C6ED4816D465}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyFlib", "MyFlib\MyFlib.csproj", "{2142AB69-B483-4B0A-96DC-CFA87DEB11A5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MyFlib", "MyFlib\MyFlib.csproj", "{2142AB69-B483-4B0A-96DC-CFA87DEB11A5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UT_EF", "Tests\UT_EF\UT_EF.csproj", "{F896C30B-A6FE-42B2-BAC8-08360BC48DC6}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -55,6 +57,10 @@ Global {2142AB69-B483-4B0A-96DC-CFA87DEB11A5}.Debug|Any CPU.Build.0 = Debug|Any CPU {2142AB69-B483-4B0A-96DC-CFA87DEB11A5}.Release|Any CPU.ActiveCfg = Release|Any CPU {2142AB69-B483-4B0A-96DC-CFA87DEB11A5}.Release|Any CPU.Build.0 = Release|Any CPU + {F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F896C30B-A6FE-42B2-BAC8-08360BC48DC6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -62,6 +68,7 @@ Global GlobalSection(NestedProjects) = preSolution {1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170} + {F896C30B-A6FE-42B2-BAC8-08360BC48DC6} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} diff --git a/src/EntityFramework_LoL/Sources/Model/Model.csproj b/src/EntityFramework_LoL/Sources/Model/Model.csproj index 27b2839..397943a 100644 --- a/src/EntityFramework_LoL/Sources/Model/Model.csproj +++ b/src/EntityFramework_LoL/Sources/Model/Model.csproj @@ -12,6 +12,9 @@ + + + diff --git a/src/EntityFramework_LoL/Sources/MyFlib/ChampionEntity.cs b/src/EntityFramework_LoL/Sources/MyFlib/ChampionEntity.cs deleted file mode 100644 index 0f25121..0000000 --- a/src/EntityFramework_LoL/Sources/MyFlib/ChampionEntity.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MyFlib -{ - public class ChampionEntity - { - [Key] - public string Name { get; set; } - public ChampionEntity(string name) - { - Name = name; - } - } -} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db b/src/EntityFramework_LoL/Sources/MyFlib/DataBase.db index 13315b347377c658e1bd2b11398592e26609210e..d146bd75f1ea62bf6e77ac0f2653cb25ce37d487 100644 GIT binary patch delta 582 zcmZozz}T>Wae}m<5Ca1PD-gqg&_o?$Q6UDscqv}~9}FzK=?r|A_}B98KNjx5aQ_M|eg((S5PwfUpjp1Iejz%OWB8=% zb$GcHfI!J9Ghay|#5E#B!OtIvLw$T;GM>r#dAOu<6Vp?1$T{aE78k?qhnvdFrP&z4 z$S!Vf&e-H%lDPREAD1AziJ^g!sgcp<_wxVv)uEht-%8)i^rFO)%=|oFV2HBuFJ<8W z#Q%u@3jY!QZTw3&3kpo+&*x`jXOQOQW%od5s; delta 234 zcmZozz}T>Wae}lU69WSSD-go~(?lI(Q6>hxcqv}~9}FyfiVS?0_}B6&^3C5YD6p27 zvxb>nTwb2B(S7n^UP~^&#N1RRg%H<>kjd Champions { get; set; } + public LolDbContext() + { } + + public LolDbContext(DbContextOptions options) : base(options) + { } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { - optionsBuilder.UseSqlite(@"Data Source=DataBase.db"); + optionsBuilder.UseSqlite("Data Source=DataBase.db"); } } protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity().HasData(new ChampionEntity("Akali")); + 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 = "" } + ); + } } diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs deleted file mode 100644 index 6ce17ec..0000000 --- a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.Designer.cs +++ /dev/null @@ -1,40 +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("20230201163620_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("Name") - .HasColumnType("TEXT"); - - b.HasKey("Name"); - - b.ToTable("Champions"); - - b.HasData( - new - { - Name = "Akali" - }); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs deleted file mode 100644 index 8716d00..0000000 --- a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230201163620_MyMigration.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace MyFlib.Migrations -{ - /// - public partial class MyMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Champions", - columns: table => new - { - Name = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Champions", x => x.Name); - }); - - migrationBuilder.InsertData( - table: "Champions", - column: "Name", - value: "Akali"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Champions"); - } - } -} diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204102522_MyMigration.Designer.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204102522_MyMigration.Designer.cs new file mode 100644 index 0000000..78d288f --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204102522_MyMigration.Designer.cs @@ -0,0 +1,114 @@ +// +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("20230204102522_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/20230204102522_MyMigration.cs b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204102522_MyMigration.cs new file mode 100644 index 0000000..2480c4e --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/20230204102522_MyMigration.cs @@ -0,0 +1,53 @@ +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 index 5f71390..76cb456 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/Migrations/LolDbContextModelSnapshot.cs +++ b/src/EntityFramework_LoL/Sources/MyFlib/Migrations/LolDbContextModelSnapshot.cs @@ -18,17 +18,91 @@ namespace MyFlib.Migrations 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("Name"); + 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/MyFlib.csproj b/src/EntityFramework_LoL/Sources/MyFlib/MyFlib.csproj index f99623c..effa3a7 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/MyFlib.csproj +++ b/src/EntityFramework_LoL/Sources/MyFlib/MyFlib.csproj @@ -16,6 +16,7 @@ + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/EntityFramework_LoL/Sources/MyFlib/Program.cs b/src/EntityFramework_LoL/Sources/MyFlib/Program.cs index ef4ef6d..3e22f35 100644 --- a/src/EntityFramework_LoL/Sources/MyFlib/Program.cs +++ b/src/EntityFramework_LoL/Sources/MyFlib/Program.cs @@ -1,10 +1,12 @@ // See https://aka.ms/new-console-template for more information using MyFlib; -Console.WriteLine("Hello, World!"); - -using(var context = new LolDbContext()) +using (var context = new LolDbContext()) { - //var champions = context.Champions.where + + foreach (var c in context.Champions) + { + Console.WriteLine($"{c.Name} - {c.Bio}"); + } context.SaveChangesAsync(); // or context.SaveChangesAsync } \ No newline at end of file diff --git a/src/EntityFramework_LoL/Sources/MyFlib/enums/ChampionClassEntity.cs b/src/EntityFramework_LoL/Sources/MyFlib/enums/ChampionClassEntity.cs new file mode 100644 index 0000000..bec72f2 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/MyFlib/enums/ChampionClassEntity.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MyFlib +{ + public enum ChampionClassEntity + { + Unknown, + Assassin, + Fighter, + Mage, + Marksman, + Support, + Tank, + } +} diff --git a/src/EntityFramework_LoL/Sources/Shared/Shared.csproj b/src/EntityFramework_LoL/Sources/Shared/Shared.csproj index bafd05b..9c33615 100644 --- a/src/EntityFramework_LoL/Sources/Shared/Shared.csproj +++ b/src/EntityFramework_LoL/Sources/Shared/Shared.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/src/EntityFramework_LoL/Sources/StubLib/StubLib.csproj b/src/EntityFramework_LoL/Sources/StubLib/StubLib.csproj index f05027c..b03fb75 100644 --- a/src/EntityFramework_LoL/Sources/StubLib/StubLib.csproj +++ b/src/EntityFramework_LoL/Sources/StubLib/StubLib.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/src/EntityFramework_LoL/Sources/Tests/ConsoleTests/ConsoleTests.csproj b/src/EntityFramework_LoL/Sources/Tests/ConsoleTests/ConsoleTests.csproj index 1602b94..3b17a32 100644 --- a/src/EntityFramework_LoL/Sources/Tests/ConsoleTests/ConsoleTests.csproj +++ b/src/EntityFramework_LoL/Sources/Tests/ConsoleTests/ConsoleTests.csproj @@ -15,6 +15,7 @@ + diff --git a/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs b/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs new file mode 100644 index 0000000..1f99d6a --- /dev/null +++ b/src/EntityFramework_LoL/Sources/Tests/UT_EF/ChampionsTest.cs @@ -0,0 +1,75 @@ + +using Microsoft.EntityFrameworkCore; +using MyFlib; +using Xunit; + +namespace UT_EF +{ + public class UnitTest1 + { + [Fact] + public void TestAdd() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Add_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.Add(sylas); + context.Champions.Add(hecarim); + context.Champions.Add(yuumi); + context.SaveChanges(); + } + + using (var context = new LolDbContext(options)) + { + Assert.Equal(3, context.Champions.Count()); + Assert.Equal("Sylas", context.Champions.First().Name); + } + } + + [Fact] + public void Modify_Test() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Modify_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.Add(sylas); + context.Champions.Add(hecarim); + context.Champions.Add(yuumi); + context.SaveChanges(); + } + + using (var context = new LolDbContext(options)) + { + string nameToFind = "m"; + Assert.Equal(2, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).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"; + context.SaveChanges(); + } + + using (var context = new LolDbContext(options)) + { + string nameToFind = "m"; + Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); + nameToFind = "garen"; + Assert.Equal(1, context.Champions.Where(c => c.Name.ToLower().Contains(nameToFind)).Count()); + } + } + } +} \ No newline at end of file diff --git a/src/EntityFramework_LoL/Sources/Tests/UT_EF/UT_EF.csproj b/src/EntityFramework_LoL/Sources/Tests/UT_EF/UT_EF.csproj new file mode 100644 index 0000000..0b8e47b --- /dev/null +++ b/src/EntityFramework_LoL/Sources/Tests/UT_EF/UT_EF.csproj @@ -0,0 +1,29 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/src/EntityFramework_LoL/Sources/Tests/UT_EF/Usings.cs b/src/EntityFramework_LoL/Sources/Tests/UT_EF/Usings.cs new file mode 100644 index 0000000..8c927eb --- /dev/null +++ b/src/EntityFramework_LoL/Sources/Tests/UT_EF/Usings.cs @@ -0,0 +1 @@ +global using Xunit; \ No newline at end of file