diff --git a/tp1/DbContextLib/Migrations/20240209010818_Mg.Designer.cs b/tp1/DbContextLib/Migrations/20240209010818_Mg.Designer.cs deleted file mode 100644 index 36c19de..0000000 --- a/tp1/DbContextLib/Migrations/20240209010818_Mg.Designer.cs +++ /dev/null @@ -1,47 +0,0 @@ -// -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("20240209010818_Mg")] - partial class Mg - { - /// - 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/20240209010818_Mg.cs b/tp1/DbContextLib/Migrations/20240209010818_Mg.cs deleted file mode 100644 index 0ce9e15..0000000 --- a/tp1/DbContextLib/Migrations/20240209010818_Mg.cs +++ /dev/null @@ -1,36 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace DbContextLib.Migrations -{ - /// - public partial class Mg : 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 deleted file mode 100644 index 61df967..0000000 --- a/tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs +++ /dev/null @@ -1,44 +0,0 @@ -// -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/Entities/BookEntity.cs b/tp1/Entities/BookEntity.cs index 01711d6..1849e64 100644 --- a/tp1/Entities/BookEntity.cs +++ b/tp1/Entities/BookEntity.cs @@ -9,17 +9,14 @@ namespace Entities public class BookEntity { - public int ID { get; set; } + public long ID { get; set; } public string Title { get; set; } public string Author { get; set; } public string Isbn { get; set; } - public PersonEntity? Person { get; set; } + public int PersonId { get; set; } + public PersonEntity? Owner { get; set; } - public BookEntity(string title, string author, string isbn) { - Title = title; - Author = author; - Isbn = isbn; - } + } } diff --git a/tp1/Entities/Entities.csproj b/tp1/Entities/Entities.csproj index bb23fb7..0c79874 100644 --- a/tp1/Entities/Entities.csproj +++ b/tp1/Entities/Entities.csproj @@ -6,4 +6,17 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/tp1/Entities/PersonEntity.cs b/tp1/Entities/PersonEntity.cs index 5f0e45c..8304147 100644 --- a/tp1/Entities/PersonEntity.cs +++ b/tp1/Entities/PersonEntity.cs @@ -8,11 +8,12 @@ namespace Entities { public class PersonEntity { - public long Id { get; set; } + public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } - public ICollection Books { get; set; } + public ICollection Books { get; set; } = new List(); + } } diff --git a/tp1/TestStub/Program.cs b/tp1/TestStub/Program.cs index b2de216..3bf9e2d 100644 --- a/tp1/TestStub/Program.cs +++ b/tp1/TestStub/Program.cs @@ -2,26 +2,32 @@ using Entities; using DbContextLib; using Microsoft.Extensions.Options; +using System.Reflection; +using Microsoft.EntityFrameworkCore.Storage; +using Microsoft.EntityFrameworkCore; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); using (var context = new LibraryContext()) { - 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.Database.EnsureCreated(); - context.BooksSet.Add(chewie); - context.BooksSet.Add(yoda); - context.BooksSet.Add(ewok); - context.BooksSet.Add(the100); + PersonEntity p1 = new PersonEntity() { FirstName = "Tom", LastName = "Rambeau" }; + PersonEntity p2 = new PersonEntity() { FirstName = "Erwan", LastName = "Manager" }; + BookEntity chewie = new BookEntity() { Title = "mistake", Author = "test1", Isbn ="test1"}; + BookEntity the100 = new BookEntity() { Title = "the100", Author = "test4", Isbn = "test4", Owner = p1 }; + BookEntity GOT = new BookEntity() { Title = "GOT", Author = "lastTest", Isbn = "lastTest"}; + + context.Add(p1); + context.Add(p2); + context.Add(chewie); + context.Add(GOT); + context.Add(the100); context.SaveChanges(); } - using (var context = new LibraryContext()) { foreach (var n in context.BooksSet) @@ -48,4 +54,82 @@ using (var context = new LibraryContext()) context.Remove(impostor); context.SaveChanges(); +} + +using (var context = new LibraryContext()) +{ + Console.WriteLine("All people"); + var people = context.PersonSet; + foreach(var p in people) + { + Console.WriteLine($"firstname: {p.FirstName}, lastname: {p.LastName}"); + } +} + +//emprunt +using (var context = new LibraryContext()) +{ + var books = context.BooksSet; + foreach(var book in books) + { + if (book.Owner == null) + { + book.Owner = context.PersonSet.Where(b => b.FirstName.StartsWith("E")).Include(b => b.Books).First(); + Console.WriteLine($" nouveau propriétaire du livre: {book.Owner.FirstName}"); + break; + } + } + + context.SaveChanges(); +} + + +//Rendu +using (var context = new LibraryContext()) +{ + + var books = context.BooksSet; + if (books != null) + { + foreach (var book in books) + { + //Console.WriteLine(book.Owner.FirstName); + if (book.Owner != null) + { + Console.WriteLine($" propriétaire du livre avant rendu : {book.Owner.FirstName}"); + book.Owner = null; + Console.WriteLine("propriétaire a rendu le libre"); + break; + } + else + { + Console.WriteLine("livre sans propriétaire" + book.Title); + } + } + } + + context.SaveChanges(); +} + + +//DeleteAllItem +using (var context = new LibraryContext()) +{ + var allBooks = context.BooksSet; + if ( allBooks != null) + { + foreach (var book in allBooks) + { + context.BooksSet.Remove(book); + } + } + var allPeople = context.PersonSet; + if (allBooks != null) + { + foreach (var person in allPeople) + { + context.PersonSet.Remove(person); + } + } + context.SaveChanges(); } \ No newline at end of file diff --git a/tp1/TestStub/tp.Books.db b/tp1/TestStub/tp.Books.db index e005c58..5fb405f 100644 Binary files a/tp1/TestStub/tp.Books.db and b/tp1/TestStub/tp.Books.db differ diff --git a/tp1/TestStub/tp.Books.db-shm b/tp1/TestStub/tp.Books.db-shm new file mode 100644 index 0000000..93ce7a3 Binary files /dev/null and b/tp1/TestStub/tp.Books.db-shm differ diff --git a/tp1/TestStub/tp.Books.db-wal b/tp1/TestStub/tp.Books.db-wal new file mode 100644 index 0000000..29d122f Binary files /dev/null and b/tp1/TestStub/tp.Books.db-wal differ