part2
Tom RAMBEAU 1 year ago
commit 368f7d1442

@ -1,47 +0,0 @@
// <auto-generated />
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
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("ID");
b.ToTable("BooksSet");
});
#pragma warning restore 612, 618
}
}
}

@ -1,36 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DbContextLib.Migrations
{
/// <inheritdoc />
public partial class Mg : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "BooksSet",
columns: table => new
{
ID = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(type: "TEXT", nullable: false),
Author = table.Column<string>(type: "TEXT", nullable: false),
Isbn = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BooksSet", x => x.ID);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BooksSet");
}
}
}

@ -1,44 +0,0 @@
// <auto-generated />
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<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("ID");
b.ToTable("BooksSet");
});
#pragma warning restore 612, 618
}
}
}

@ -9,17 +9,14 @@ namespace Entities
public class BookEntity public class BookEntity
{ {
public int ID { get; set; } public long ID { get; set; }
public string Title { get; set; } public string Title { get; set; }
public string Author { get; set; } public string Author { get; set; }
public string Isbn { 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;
}
} }
} }

@ -6,4 +6,17 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project> </Project>

@ -8,11 +8,12 @@ namespace Entities
{ {
public class PersonEntity public class PersonEntity
{ {
public long Id { get; set; } public int Id { get; set; }
public string FirstName { get; set; } public string FirstName { get; set; }
public string LastName { get; set; } public string LastName { get; set; }
public ICollection<BookEntity> Books { get; set; } public ICollection<BookEntity> Books { get; set; } = new List<BookEntity>();
} }
} }

@ -1,47 +0,0 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using StubbedContextLib;
#nullable disable
namespace StubbedContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
[Migration("20240209013745_Mg")]
partial class Mg
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<int>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("ID");
b.ToTable("BooksSet");
});
#pragma warning restore 612, 618
}
}
}

@ -1,36 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace StubbedContextLib.Migrations
{
/// <inheritdoc />
public partial class Mg : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "BooksSet",
columns: table => new
{
ID = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(type: "TEXT", nullable: false),
Author = table.Column<string>(type: "TEXT", nullable: false),
Isbn = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_BooksSet", x => x.ID);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BooksSet");
}
}
}

@ -0,0 +1,89 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using StubbedContextLib;
#nullable disable
namespace StubbedContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
[Migration("20240209155948_Mg")]
partial class Mg
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<long>("ID")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int?>("OwnerId")
.HasColumnType("INTEGER");
b.Property<int>("PersonId")
.HasColumnType("INTEGER");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("ID");
b.HasIndex("OwnerId");
b.ToTable("BooksSet");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("PersonSet");
});
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.HasOne("Entities.PersonEntity", "Owner")
.WithMany("Books")
.HasForeignKey("OwnerId");
b.Navigation("Owner");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Navigation("Books");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,65 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace StubbedContextLib.Migrations
{
/// <inheritdoc />
public partial class Mg : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PersonSet",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
FirstName = table.Column<string>(type: "TEXT", nullable: false),
LastName = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PersonSet", x => x.Id);
});
migrationBuilder.CreateTable(
name: "BooksSet",
columns: table => new
{
ID = table.Column<long>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Title = table.Column<string>(type: "TEXT", nullable: false),
Author = table.Column<string>(type: "TEXT", nullable: false),
Isbn = table.Column<string>(type: "TEXT", nullable: false),
PersonId = table.Column<int>(type: "INTEGER", nullable: false),
OwnerId = table.Column<int>(type: "INTEGER", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_BooksSet", x => x.ID);
table.ForeignKey(
name: "FK_BooksSet_PersonSet_OwnerId",
column: x => x.OwnerId,
principalTable: "PersonSet",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_BooksSet_OwnerId",
table: "BooksSet",
column: "OwnerId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "BooksSet");
migrationBuilder.DropTable(
name: "PersonSet");
}
}
}

@ -1,44 +1,86 @@
// <auto-generated /> // <auto-generated />
using Microsoft.EntityFrameworkCore; using System;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using Microsoft.EntityFrameworkCore.Infrastructure;
using StubbedContextLib; using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using StubbedContextLib;
#nullable disable
#nullable disable
namespace StubbedContextLib.Migrations
{ namespace StubbedContextLib.Migrations
[DbContext(typeof(StubbedContext))] {
partial class StubbedContextModelSnapshot : ModelSnapshot [DbContext(typeof(StubbedContext))]
{ partial class StubbedContextModelSnapshot : ModelSnapshot
protected override void BuildModel(ModelBuilder modelBuilder) {
{ protected override void BuildModel(ModelBuilder modelBuilder)
#pragma warning disable 612, 618 {
modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); #pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Entities.BookEntity", b =>
{ modelBuilder.Entity("Entities.BookEntity", b =>
b.Property<int>("ID") {
.ValueGeneratedOnAdd() b.Property<long>("ID")
.HasColumnType("INTEGER"); .ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Author")
.IsRequired() b.Property<string>("Author")
.HasColumnType("TEXT"); .IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Isbn")
.IsRequired() b.Property<string>("Isbn")
.HasColumnType("TEXT"); .IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Title")
.IsRequired() b.Property<int?>("OwnerId")
.HasColumnType("TEXT"); .HasColumnType("INTEGER");
b.HasKey("ID"); b.Property<int>("PersonId")
.HasColumnType("INTEGER");
b.ToTable("BooksSet");
}); b.Property<string>("Title")
#pragma warning restore 612, 618 .IsRequired()
} .HasColumnType("TEXT");
}
} b.HasKey("ID");
b.HasIndex("OwnerId");
b.ToTable("BooksSet");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("PersonSet");
});
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.HasOne("Entities.PersonEntity", "Owner")
.WithMany("Books")
.HasForeignKey("OwnerId");
b.Navigation("Owner");
});
modelBuilder.Entity("Entities.PersonEntity", b =>
{
b.Navigation("Books");
});
#pragma warning restore 612, 618
}
}
}

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -15,8 +16,14 @@ namespace StubbedContextLib
public List<BookEntity> Books { get; set; } = new List<BookEntity>(); public List<BookEntity> Books { get; set; } = new List<BookEntity>();
public StubbedContext() { public StubbedContext() {
Books.Add(new BookEntity("test", "test", "test")); BookEntity b0 = new BookEntity() { Title = "test", Author = "test", Isbn = "test" };
Books.Add(new BookEntity("test2", "test2", "test2")); BookEntity b1 = new BookEntity() { Title = "test2", Author = "test2", Isbn = "test2" };
Books.Add(b0);
Books.Add(b1);
Collection<BookEntity> CB = new Collection<BookEntity>();
CB.Add(b0);
CB.Add(b1);
Persons.Add(new PersonEntity() { FirstName = "coco", LastName = "test", Books = CB });
} }
} }

@ -2,26 +2,33 @@
using Entities; using Entities;
using DbContextLib; using DbContextLib;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore;
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
BookEntity chewie = new BookEntity("B3", "test1", "test1");
BookEntity princeOfPersia = new BookEntity ("B4", "test2", "test2");
BookEntity PercyJackson = new BookEntity ("mistake", "test3", "test3");
BookEntity the100 = new BookEntity("the100", "test4", "test4");
context.BooksSet.Add(chewie); context.Database.EnsureCreated();
context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson); PersonEntity p1 = new PersonEntity() { FirstName = "Tom", LastName = "Rambeau" };
context.BooksSet.Add(the100); 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(); context.SaveChanges();
} }
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
foreach (var n in context.BooksSet) foreach (var n in context.BooksSet)
@ -48,4 +55,82 @@ using (var context = new LibraryContext())
context.Remove(impostor); context.Remove(impostor);
context.SaveChanges(); 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();
} }

Binary file not shown.

@ -25,7 +25,7 @@ namespace UnitTests
//context.Database.OpenConnection(); //context.Database.OpenConnection();
context.Database.EnsureCreated(); context.Database.EnsureCreated();
BookEntity the100 = new BookEntity( "the100", "", "" ); BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "" };
BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" ); BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" );
BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" ); BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" );
context.BooksSet.Add(the100); context.BooksSet.Add(the100);

Loading…
Cancel
Save