part1
Clement LESME 1 year ago
parent 7cd035c6c6
commit 2ca4b8ba05

@ -1,2 +1,70 @@
// See https://aka.ms/new-console-template for more information using listBooksEF;
Console.WriteLine("Hello, World!"); using Microsoft.Extensions.DependencyModel;
using Microsoft.Identity.Client;
using StubbedContextLib;
using System.Diagnostics;
using Xunit;
List<BookEntity> books = new List<BookEntity>();
StubbedContext myStub = new StubbedContext();
// Insertion de livres dans la base de données
using (var context = new LibraryContext())
{
Console.WriteLine("Insertion\n");
books = await myStub.booksStub();
// Ajoute chaque livre à la base de données de manière asynchrone
foreach (var book in books)
{
await context.Books.AddAsync(book);
}
// Enregistre les modifications dans la base de données
await context.SaveChangesAsync();
}
// Récupération et affichage des livres de la base de données
using (var context = new LibraryContext())
{
Console.WriteLine("Récupération\n");
foreach (var book in context.Books)
{
Console.WriteLine($"{book.Title}, Auteur : {book.Author}");
}
}
// Suppression d'un livre avec l'ID 2 de la base de données
using (var context = new LibraryContext())
{
Console.WriteLine("Suppression\n");
var book = context.Books.FirstOrDefault(b => b.Id == 2);
if (book != null)
{
context.Books.Remove(book);
await context.SaveChangesAsync();
}
}
// Modification du titre d'un livre avec l'auteur "Martin"
using (var context = new LibraryContext())
{
Console.WriteLine("Modification\n");
var bookToUpdate = context.Books.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);
}

@ -1,47 +0,0 @@
// <auto-generated />
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("20240122161115_testMigration")]
partial class testMigration
{
/// <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<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 ContextLib.Migrations
{
/// <inheritdoc />
public partial class testMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
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)
},
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 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<long>("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
}
}
}

Binary file not shown.

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StubContext
{
public class StubContext : LibraryContext
{
public async Task<List<BookEntity>> booksStub()
{
List<BookEntity> listBook = new List<BookEntity>();
//Création des livres
BookEntity book1 = new BookEntity
{
Author = "Lechardeur",
Title = "Comment pécho 100% garanti",
Isbn = "696969a"
};
BookEntity book2 = new BookEntity
{
Author = "Vianney",
Title = "Gros",
Isbn = "0015150"
};
BookEntity book3 = new BookEntity
{
Author = "Khéna",
Title = "Khéna; un homme, un livre",
Isbn = "666666b"
};
//Ajout des livres
books.Add(book1);
books.Add(book2);
books.Add(book3);
return listBook;
}
}
}
Loading…
Cancel
Save