diff --git a/App/App.csproj b/App/App.csproj index f145d4a..8446478 100644 --- a/App/App.csproj +++ b/App/App.csproj @@ -19,6 +19,7 @@ + diff --git a/App/App.sln b/App/App.sln index 6576f1b..fa09de1 100644 --- a/App/App.sln +++ b/App/App.sln @@ -3,24 +3,24 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34330.188 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "App", "App.csproj", "{E8B1B649-B0B8-4DDF-BE5E-7EB74EE8E79A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "App.csproj", "{E8B1B649-B0B8-4DDF-BE5E-7EB74EE8E79A}" ProjectSection(ProjectDependencies) = postProject {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "..\Entities\Entities.csproj", "{A3AD5ED6-188F-4F13-8B43-F649502A9DED}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "..\Entities\Entities.csproj", "{A3AD5ED6-188F-4F13-8B43-F649502A9DED}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContextLib", "..\ContextLib\ContextLib.csproj", "{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContextLib", "..\ContextLib\ContextLib.csproj", "{2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A}" ProjectSection(ProjectDependencies) = postProject {A3AD5ED6-188F-4F13-8B43-F649502A9DED} = {A3AD5ED6-188F-4F13-8B43-F649502A9DED} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubContext", "..\StubContext\StubContext.csproj", "{98FB6B00-686C-4B31-B000-876B08E5DAEA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "..\StubContext\StubbedContextLib.csproj", "{98FB6B00-686C-4B31-B000-876B08E5DAEA}" ProjectSection(ProjectDependencies) = postProject {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} = {2A160FE9-4C72-4BCD-A9B0-5E4F894FE22A} EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "..\Test\Test.csproj", "{E8159099-8CD9-49FF-A5D5-95B651756C19}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test", "..\Test\Test.csproj", "{E8159099-8CD9-49FF-A5D5-95B651756C19}" ProjectSection(ProjectDependencies) = postProject {98FB6B00-686C-4B31-B000-876B08E5DAEA} = {98FB6B00-686C-4B31-B000-876B08E5DAEA} EndProjectSection diff --git a/App/Program.cs b/App/Program.cs index 7bdef8d..3f12144 100644 --- a/App/Program.cs +++ b/App/Program.cs @@ -1,12 +1,54 @@ -using listBooksEF; +using ContextLib; +using Entities; using Microsoft.Extensions.DependencyModel; -using Microsoft.Identity.Client; -using StubbedContextLib; using System.Diagnostics; -using Xunit; +using StubbedContextLib; +/* +namespace App +{ + class Program + { + static void Main(string[] args) + { + BookEntity book2 = new BookEntity + { + Id = 2, + Author = "Vianney", + Title = "Gros", + Isbn = "0015150" + }; + + BookEntity book3 = new BookEntity + { + Id = 3, + Author = "Khéna", + Title = "Khéna. Un homme, un livre", + Isbn = "666666b" + }; + + using (var context = new LibraryContext()) + { + Console.WriteLine("insertion"); + context.Add(book2); + context.Add(book3); + context.SaveChanges(); + } + + using (var context = new LibraryContext()) + { + foreach (var n in context.BooksSet) + { + Console.WriteLine($"{n.Id} - {n.Author}"); + } + context.SaveChanges(); + } + } + } +}*/ + List books = new List(); -StubbedContext myStub = new StubbedContext(); +StubContext myStub = new StubContext(); // Insertion de livres dans la base de données using (var context = new LibraryContext()) @@ -18,7 +60,7 @@ using (var context = new LibraryContext()) // Ajoute chaque livre à la base de données de manière asynchrone foreach (var book in books) { - await context.Books.AddAsync(book); + await context.BooksSet.AddAsync(book); } // Enregistre les modifications dans la base de données @@ -30,7 +72,7 @@ using (var context = new LibraryContext()) { Console.WriteLine("Récupération\n"); - foreach (var book in context.Books) + foreach (var book in context.BooksSet) { Console.WriteLine($"{book.Title}, Auteur : {book.Author}"); } @@ -41,10 +83,10 @@ using (var context = new LibraryContext()) { Console.WriteLine("Suppression\n"); - var book = context.Books.FirstOrDefault(b => b.Id == 2); + var book = context.BooksSet.FirstOrDefault(b => b.Id == 2); if (book != null) { - context.Books.Remove(book); + context.BooksSet.Remove(book); await context.SaveChangesAsync(); } } @@ -54,17 +96,10 @@ using (var context = new LibraryContext()) { Console.WriteLine("Modification\n"); - var bookToUpdate = context.Books.FirstOrDefault(b => b.Author == "Martin"); + var bookToUpdate = context.BooksSet.FirstOrDefault(b => b.Author == "Martin"); if (bookToUpdate != null) { bookToUpdate.Title = "Soir"; await context.SaveChangesAsync(); } -} - -// Assertion sur le nombre de livres dans la base de données -using (var context = new LibraryContext()) -{ - var cpt = context.Books.Count(); - Assert.Equal(cpt, 2); } \ No newline at end of file diff --git a/ContextLib/LibraryContext.cs b/ContextLib/LibraryContext.cs index 62b223c..163b408 100644 --- a/ContextLib/LibraryContext.cs +++ b/ContextLib/LibraryContext.cs @@ -5,12 +5,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Entities; +using Microsoft.EntityFrameworkCore.Metadata; namespace ContextLib { public class LibraryContext : DbContext { public DbSet BooksSet { get; set; } + public DbSet PersonsSet { get; set; } public LibraryContext() { } diff --git a/ContextLib/Migrations/20240126215342_deuxiemeTestMigration.Designer.cs b/ContextLib/Migrations/20240126215342_deuxiemeTestMigration.Designer.cs deleted file mode 100644 index 54dc176..0000000 --- a/ContextLib/Migrations/20240126215342_deuxiemeTestMigration.Designer.cs +++ /dev/null @@ -1,47 +0,0 @@ -// -using ContextLib; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace ContextLib.Migrations -{ - [DbContext(typeof(LibraryContext))] - [Migration("20240126215342_deuxiemeTestMigration")] - partial class deuxiemeTestMigration - { - /// - 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/ContextLib/Migrations/20240126215342_deuxiemeTestMigration.cs b/ContextLib/Migrations/20240126215342_deuxiemeTestMigration.cs deleted file mode 100644 index 98b40da..0000000 --- a/ContextLib/Migrations/20240126215342_deuxiemeTestMigration.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace ContextLib.Migrations -{ - /// - public partial class deuxiemeTestMigration : 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/ContextLib/Migrations/20240126215524_seuxiemeTestMigrationn.Designer.cs b/ContextLib/Migrations/20240126215524_seuxiemeTestMigrationn.Designer.cs deleted file mode 100644 index e6269f8..0000000 --- a/ContextLib/Migrations/20240126215524_seuxiemeTestMigrationn.Designer.cs +++ /dev/null @@ -1,47 +0,0 @@ -// -using ContextLib; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace ContextLib.Migrations -{ - [DbContext(typeof(LibraryContext))] - [Migration("20240126215524_seuxiemeTestMigrationn")] - partial class seuxiemeTestMigrationn - { - /// - 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/ContextLib/Migrations/20240126215524_seuxiemeTestMigrationn.cs b/ContextLib/Migrations/20240126215524_seuxiemeTestMigrationn.cs deleted file mode 100644 index 60a27d0..0000000 --- a/ContextLib/Migrations/20240126215524_seuxiemeTestMigrationn.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace ContextLib.Migrations -{ - /// - public partial class seuxiemeTestMigrationn : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - - } - } -} diff --git a/ContextLib/Migrations/LibraryContextModelSnapshot.cs b/ContextLib/Migrations/LibraryContextModelSnapshot.cs deleted file mode 100644 index 3fb3450..0000000 --- a/ContextLib/Migrations/LibraryContextModelSnapshot.cs +++ /dev/null @@ -1,44 +0,0 @@ -// -using ContextLib; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace ContextLib.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/Entities/BookEntity.cs b/Entities/BookEntity.cs index d16a37e..ac42a62 100644 --- a/Entities/BookEntity.cs +++ b/Entities/BookEntity.cs @@ -8,10 +8,17 @@ namespace Entities { public class BookEntity { - public long id { get; set; } + public long Id { get; set; } + public string Title { get; set; } + public string Author { get; set; } + public string Isbn { get; set; } + + public long? PersonEntityId { get; set; } + + public PersonEntity? Owner { get; set; } = null!; } } diff --git a/Entities/PersonEntity.cs b/Entities/PersonEntity.cs new file mode 100644 index 0000000..c779c98 --- /dev/null +++ b/Entities/PersonEntity.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Entities +{ + public class PersonEntity + { + public long Id { get; set; } + + public string FirstName { get; set; } + + public string LastName { get; set; } + + public ICollection Library { get; set; } = new List(); + } +} diff --git a/StubContext/StubContext.cs b/StubContext/StubContext.cs index 03c4775..a96badb 100644 --- a/StubContext/StubContext.cs +++ b/StubContext/StubContext.cs @@ -1,10 +1,12 @@ -using System; +using ContextLib; +using Entities; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace StubContext +namespace StubbedContextLib { public class StubContext : LibraryContext { @@ -15,6 +17,7 @@ namespace StubContext //Création des livres BookEntity book1 = new BookEntity { + Id = 1, Author = "Lechardeur", Title = "Comment pécho 100% garanti", Isbn = "696969a" @@ -22,6 +25,7 @@ namespace StubContext BookEntity book2 = new BookEntity { + Id = 2, Author = "Vianney", Title = "Gros", Isbn = "0015150" @@ -29,15 +33,16 @@ namespace StubContext BookEntity book3 = new BookEntity { + Id = 3, Author = "Khéna", Title = "Khéna; un homme, un livre", Isbn = "666666b" }; //Ajout des livres - books.Add(book1); - books.Add(book2); - books.Add(book3); + listBook.Add(book1); + listBook.Add(book2); + listBook.Add(book3); return listBook; } diff --git a/StubContext/StubContext.csproj b/StubContext/StubbedContextLib.csproj similarity index 100% rename from StubContext/StubContext.csproj rename to StubContext/StubbedContextLib.csproj diff --git a/Test/Test.cs b/Test/Test.cs new file mode 100644 index 0000000..cf778ea --- /dev/null +++ b/Test/Test.cs @@ -0,0 +1,16 @@ +using Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + + + public class Test + { + + } +} diff --git a/Test/Test.csproj b/Test/Test.csproj index 9dd1a77..88f576d 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ - +