From 90b016f200a59c9052b6d2f0bfac810673f82775 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 1 Feb 2024 15:51:53 +0100 Subject: [PATCH] ajouts migrations fonctionnelles --- .../20240201082456_BooksMigration.Designer.cs | 47 ++++++++++++++++++ .../20240201082456_BooksMigration.cs | 36 ++++++++++++++ .../Migrations/LibraryContextModelSnapshot.cs | 44 ++++++++++++++++ tp1/DbContextLib/tp1.Books.db | Bin 0 -> 20480 bytes tp1/Library/DbContextLib/LibraryContext.cs | 2 +- tp1/Library/tp.Books.db | Bin 0 -> 20480 bytes tp1/TestStub/BookEntity.cs | 23 +++++++++ tp1/TestStub/LibraryContext.cs | 22 ++++++++ ...20240201143359_BooksMigrations.Designer.cs | 47 ++++++++++++++++++ .../20240201143359_BooksMigrations.cs | 36 ++++++++++++++ .../Migrations/LibraryContextModelSnapshot.cs | 44 ++++++++++++++++ tp1/TestStub/Program.cs | 33 +++++++----- tp1/TestStub/StubbedContext.cs | 22 ++++++++ tp1/TestStub/tp.Books.db | Bin 0 -> 20480 bytes tp1/TestStubEF/Class1.cs | 7 --- tp1/TestStubEF/TestStubEF.csproj | 9 ---- tp1/tp1/Program.cs | 4 -- tp1/tp1/TestStubEF.csproj | 29 ----------- 18 files changed, 343 insertions(+), 62 deletions(-) create mode 100644 tp1/DbContextLib/Migrations/20240201082456_BooksMigration.Designer.cs create mode 100644 tp1/DbContextLib/Migrations/20240201082456_BooksMigration.cs create mode 100644 tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs create mode 100644 tp1/DbContextLib/tp1.Books.db create mode 100644 tp1/Library/tp.Books.db create mode 100644 tp1/TestStub/BookEntity.cs create mode 100644 tp1/TestStub/LibraryContext.cs create mode 100644 tp1/TestStub/Migrations/20240201143359_BooksMigrations.Designer.cs create mode 100644 tp1/TestStub/Migrations/20240201143359_BooksMigrations.cs create mode 100644 tp1/TestStub/Migrations/LibraryContextModelSnapshot.cs create mode 100644 tp1/TestStub/StubbedContext.cs create mode 100644 tp1/TestStub/tp.Books.db delete mode 100644 tp1/TestStubEF/Class1.cs delete mode 100644 tp1/TestStubEF/TestStubEF.csproj delete mode 100644 tp1/tp1/Program.cs delete mode 100644 tp1/tp1/TestStubEF.csproj diff --git a/tp1/DbContextLib/Migrations/20240201082456_BooksMigration.Designer.cs b/tp1/DbContextLib/Migrations/20240201082456_BooksMigration.Designer.cs new file mode 100644 index 0000000..69346df --- /dev/null +++ b/tp1/DbContextLib/Migrations/20240201082456_BooksMigration.Designer.cs @@ -0,0 +1,47 @@ +// +using DbContextLib; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DbContextLib.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20240201082456_BooksMigration")] + partial class BooksMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); + + modelBuilder.Entity("Entities.BookEntity", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Author") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Isbn") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Title") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("ID"); + + b.ToTable("BooksSet"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/tp1/DbContextLib/Migrations/20240201082456_BooksMigration.cs b/tp1/DbContextLib/Migrations/20240201082456_BooksMigration.cs new file mode 100644 index 0000000..2e153c8 --- /dev/null +++ b/tp1/DbContextLib/Migrations/20240201082456_BooksMigration.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DbContextLib.Migrations +{ + /// + public partial class BooksMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "BooksSet", + columns: table => new + { + ID = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Title = table.Column(type: "TEXT", nullable: false), + Author = table.Column(type: "TEXT", nullable: false), + Isbn = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BooksSet", x => x.ID); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BooksSet"); + } + } +} diff --git a/tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs b/tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs new file mode 100644 index 0000000..61df967 --- /dev/null +++ b/tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs @@ -0,0 +1,44 @@ +// +using DbContextLib; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DbContextLib.Migrations +{ + [DbContext(typeof(LibraryContext))] + partial class LibraryContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); + + modelBuilder.Entity("Entities.BookEntity", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Author") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Isbn") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Title") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("ID"); + + b.ToTable("BooksSet"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/tp1/DbContextLib/tp1.Books.db b/tp1/DbContextLib/tp1.Books.db new file mode 100644 index 0000000000000000000000000000000000000000..0ba5d307da1087e14d507ce396c2d0aa49da740e GIT binary patch literal 20480 zcmeI&Jx|*}7zgk>CqRkhrkf|ElO-e&C~{It7;C6WEfObnj*6(NEJI9dHQ2(wNHEfV zlkWX8-8yye+<}ox8uMZ~-9i6HKKWkkC%&J(_?-T`b|?aN8YgGIU@yrFqA27wV}uYz zmIYa|n6%4TL+NP2=-^ufz)gS6#SwTPm0uX=z1Rwwb2tWV=5P(2| zK(D;0ZEVni@Q+79dOnh`YhD_hkAvtW-(1*pEt6Y}o4aj`<&Is8{Ig(Fp1FSYp|qr} ztk7@E)113;{3Y!NBHNcAWYM#3ovo=ACPTO1=*)Jw)v{dXbUAYl+ikYjb^6>jWk1t< z2j1OTJ=gA-?tmRw17;rbuI9R3%EI!A{oIyK1j&eh4 zW&P@iE@{ms{o0+*&hxDOPWUPDMHolv+b|Vza#`4^Je+soF?R^JIqhNY8uAo#65Vsj zX4^~R;rK+n50X?~Y%Z;wnyoBqADfhf(J;7}-Clnz;%VC}{2i}e*jJ>RJTK!GrhJXMBAOHafKmY;|fB*y_009U<;BgADQfv9YivW}F|ME}&2nav`0uX=z u1Rwwb2tWV=5P$##p0EJk|DW*9#nd1G0SG_<0uX=z1Rwwb2tWV=Gl3iQE!=GY literal 0 HcmV?d00001 diff --git a/tp1/Library/DbContextLib/LibraryContext.cs b/tp1/Library/DbContextLib/LibraryContext.cs index 5918c9d..853f9cc 100644 --- a/tp1/Library/DbContextLib/LibraryContext.cs +++ b/tp1/Library/DbContextLib/LibraryContext.cs @@ -15,7 +15,7 @@ namespace Library.DbContextLib protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseSqlite($"Data Source=tp1.Books.db"); + optionsBuilder.UseSqlite("Data Source=tp.Books.db"); } } diff --git a/tp1/Library/tp.Books.db b/tp1/Library/tp.Books.db new file mode 100644 index 0000000000000000000000000000000000000000..74136d85c27cf0598f5fee8679ebaacb95356643 GIT binary patch literal 20480 zcmeI&J#W)M7zgk>Cv9mY6E;tk4iBhRtH=qF(h;{DU^Q`qC$kgt#x3!`|xu$S2>6{lxdP7oRhH-HoJRr%8GqO7@gIBAP~?F-8c{ zR9R6ak6F8#H`Jcqs4uH(q;dL7um2%c{VRF+z5Y%AtttozKmY;|fB*y_009U<00Iyw z5g1fgjqPnZlHu`0WET_lx)x;OVk+X3VsmBR6acDro9?+$&>R{hKz z90b>A4Lql3dn0zhN6bF-`;M#f_qgltEYkUroCuTo{79u*q|3+bseGTLO9q|nI4%t7 zwXMq|x@L4b^mBhcyCC2%deK=L$|#AmS5YRD^iyf4c6;8X$6O;^<#fh{Yba7INp!;{ zpY0$`#?uq|Mx>d#*g{%0HAh=D-gPL6;<5O+xV_<2Ci8Ys`a40hw7a4nv1yX zG;3;JwEmN*4FLfNKmY;|fB*y_009U<00Izzz?~F$TyfU_zX&k<{;&S@kAMIKAOHaf vKmY;|fB*y_009U<;2sO${r?`{TucoD5P$##AOHafKmY;|fB*y_un_nQi BooksSet { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=tp.Books.db"); + } + + } +} diff --git a/tp1/TestStub/Migrations/20240201143359_BooksMigrations.Designer.cs b/tp1/TestStub/Migrations/20240201143359_BooksMigrations.Designer.cs new file mode 100644 index 0000000..c4db459 --- /dev/null +++ b/tp1/TestStub/Migrations/20240201143359_BooksMigrations.Designer.cs @@ -0,0 +1,47 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TestStub; + +#nullable disable + +namespace TestStub.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20240201143359_BooksMigrations")] + partial class BooksMigrations + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); + + modelBuilder.Entity("TestStub.BookEntity", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Author") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Isbn") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Title") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("ID"); + + b.ToTable("BooksSet"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/tp1/TestStub/Migrations/20240201143359_BooksMigrations.cs b/tp1/TestStub/Migrations/20240201143359_BooksMigrations.cs new file mode 100644 index 0000000..1a50d42 --- /dev/null +++ b/tp1/TestStub/Migrations/20240201143359_BooksMigrations.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TestStub.Migrations +{ + /// + public partial class BooksMigrations : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "BooksSet", + columns: table => new + { + ID = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Title = table.Column(type: "TEXT", nullable: false), + Author = table.Column(type: "TEXT", nullable: false), + Isbn = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_BooksSet", x => x.ID); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BooksSet"); + } + } +} diff --git a/tp1/TestStub/Migrations/LibraryContextModelSnapshot.cs b/tp1/TestStub/Migrations/LibraryContextModelSnapshot.cs new file mode 100644 index 0000000..8c1c247 --- /dev/null +++ b/tp1/TestStub/Migrations/LibraryContextModelSnapshot.cs @@ -0,0 +1,44 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TestStub; + +#nullable disable + +namespace TestStub.Migrations +{ + [DbContext(typeof(LibraryContext))] + partial class LibraryContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); + + modelBuilder.Entity("TestStub.BookEntity", b => + { + b.Property("ID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Author") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Isbn") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Title") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("ID"); + + b.ToTable("BooksSet"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/tp1/TestStub/Program.cs b/tp1/TestStub/Program.cs index 8a17720..7d494f6 100644 --- a/tp1/TestStub/Program.cs +++ b/tp1/TestStub/Program.cs @@ -1,6 +1,4 @@ -using Library; -using Library.DbContextLib; -using Library.Entities; +using TestStub; using Microsoft.Extensions.Options; // See https://aka.ms/new-console-template for more information @@ -8,13 +6,16 @@ Console.WriteLine("Hello, World!"); using (var context = new LibraryContext()) { - BookEntity chewie = new BookEntity("B3", "test", "test2"); - BookEntity yoda = new BookEntity ("B4", "test", "test2"); - BookEntity ewok = new BookEntity ("C5", "test", "test3"); + BookEntity chewie = new BookEntity("B3", "test1", "test1"); + BookEntity yoda = new BookEntity ("B4", "test2", "test2"); + BookEntity ewok = new BookEntity ("mistake", "test3", "test3"); + BookEntity the100 = new BookEntity("the100", "test4", "test4"); context.BooksSet.Add(chewie); context.BooksSet.Add(yoda); context.BooksSet.Add(ewok); + context.BooksSet.Add(the100); + context.SaveChanges(); } @@ -23,18 +24,26 @@ using (var context = new LibraryContext()) { foreach (var n in context.BooksSet) { - Console.WriteLine($"{n.ID} - {n.Title}"); + Console.WriteLine($"Books: {n.ID} - {n.Title}"); } - context.SaveChanges(); - } + using (var context = new LibraryContext()) { - var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("B")).First(); - Console.WriteLine($"{eBooks.Title} (born in {eBooks.Author})"); + var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("t")).First(); + Console.WriteLine($"{eBooks.Title} (made by {eBooks.Author})"); + eBooks.Title = "Border"; + context.SaveChanges(); +} - eBooks.Title = "Wicket"; +using (var context = new LibraryContext()) +{ + Console.WriteLine("Deletes one item from de database"); + var impostor = context.BooksSet + .SingleOrDefault(n => n.Title.Equals("mistake")); + context.Remove(impostor); context.SaveChanges(); + } \ No newline at end of file diff --git a/tp1/TestStub/StubbedContext.cs b/tp1/TestStub/StubbedContext.cs new file mode 100644 index 0000000..f397ada --- /dev/null +++ b/tp1/TestStub/StubbedContext.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Library.DbContextLib; +using Library.Entities; +using Microsoft.EntityFrameworkCore; + +namespace TestStub +{ + public class StubbedContext : DbContext + { + public List Books { get; set; } + + public StubbedContext() { + Books.Add(new BookEntity("test", "test", "test")); + Books.Add(new BookEntity("test2", "test2", "test2")); + } + + } +} diff --git a/tp1/TestStub/tp.Books.db b/tp1/TestStub/tp.Books.db new file mode 100644 index 0000000000000000000000000000000000000000..9b976f72c8e32c5b33a8a142f0d5c825a0a427c6 GIT binary patch literal 20480 zcmeI)Jx|*}7zgmXybz$63sR6%w)6bp!j?MUrN zJ9g=3VDHvX(Xms9svn?3g;0l%y*QW`%jq8Je{%ft+}-25pS{>hvhku~`xf7K+~cOt zizG`ZB~Lgfgh=9(7N4*r#7-(a5L@P`4XJq^v`z-wtJ17u<00bZa z0SG_<0uX=z1RxM2P)`lXb8~dZZ|)vh-pP@8Tr)lEq+_-BqKD~~hE_5(Zj{OujYrzf zw42A)yy!W5`YM@`r>E(MksxQ;aSpu=%MZ7yTUqj9IL~LKQ7%eef12ld)zF@64PLDo zyt-Mb@ReG1!)TO5oagIn=5?=nLtig7cKDjM!%Le+O|Odl>sr;Azf5P?{*jgEhPEwI zU8W1`N*({ear;#2-flZmC5_IWZqtmsyiDKMg4vm-_H5mL<2HTUX?v@-=R5AZ*kI!J zykpy3BV6UwTahtDDf%S3;S$ca?mDf`p1)3H!oK_Lco+XRO0Mu^&uj6?T98TvVYz00Izz00bZa0SG_<0uX=z z1O`rElBLN#S@sVsrBLuK&tD8yY?a)dlI3IZ^MK}|)eRMc)r6EL6S7lyBN4ji2*|Z0uX=z1Rwwb2tWV= U5P$##AkbG}j3%dP`1cI#A1X#Pt^fc4 literal 0 HcmV?d00001 diff --git a/tp1/TestStubEF/Class1.cs b/tp1/TestStubEF/Class1.cs deleted file mode 100644 index 7e68d8e..0000000 --- a/tp1/TestStubEF/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace TestStubEF -{ - public class Class1 - { - - } -} diff --git a/tp1/TestStubEF/TestStubEF.csproj b/tp1/TestStubEF/TestStubEF.csproj deleted file mode 100644 index bb23fb7..0000000 --- a/tp1/TestStubEF/TestStubEF.csproj +++ /dev/null @@ -1,9 +0,0 @@ - - - - net8.0 - enable - enable - - - diff --git a/tp1/tp1/Program.cs b/tp1/tp1/Program.cs deleted file mode 100644 index bb851ad..0000000 --- a/tp1/tp1/Program.cs +++ /dev/null @@ -1,4 +0,0 @@ -using Microsoft.EntityFrameworkCore.Design; - -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); diff --git a/tp1/tp1/TestStubEF.csproj b/tp1/tp1/TestStubEF.csproj deleted file mode 100644 index e4b7805..0000000 --- a/tp1/tp1/TestStubEF.csproj +++ /dev/null @@ -1,29 +0,0 @@ - - - - Exe - net8.0 - enable - enable - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - -