Compare commits

...

15 Commits

@ -1,2 +1,10 @@
# tp1Entity
Le répot correspond au projet d'EntityFrameWork.
Vous y trouverez le tp1 dans la branche part1.
Concernant l'organisation de dépôt, un choix temporaire a été réalisé
de mêttre dans toutes les classes dans le projet TestStub qui
correpond à l'application console. Ce choix a été
réalisé pour régler un problème de migrations.

@ -1,21 +1,68 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Entities;
using Microsoft.Extensions.Options;
namespace DbContextLib
{
public class LibraryContext: DbContext
public class LibraryContext : DbContext
{
public DbSet<BookEntity> BooksSet{ get; set; }
public DbSet<PersonEntity> PersonSet { get; set; }
public DbSet<BookEntity> BooksSet { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Data Source=tp1.Books.db");
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite("Data Source=tp.Books.db");
}
}
public LibraryContext()
{ }
public LibraryContext(DbContextOptions <LibraryContext> options )
: base(options)
{ }
public void AddBook(BookEntity book)
{
if (!BooksSet.Contains(book))
{
BooksSet.Add(book);
}
}
}
public void AddPerson(PersonEntity person)
{
if (!PersonSet.Contains(person))
{
PersonSet.Add(person);
}
}
public void RemoveAll()
{
if (BooksSet.Count() != 0)
{
foreach (var book in BooksSet)
{
BooksSet.Remove(book);
}
}
if (PersonSet.Count() != 0)
{
foreach (var person in PersonSet)
{
PersonSet.Remove(person);
}
}
}
}
}

@ -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("20240201082456_BooksMigration")]
partial class BooksMigration
{
/// <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 BooksMigration : 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 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("20240216040156_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 DbContextLib.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,4 +1,5 @@
// <auto-generated />
using System;
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
@ -18,7 +19,7 @@ namespace DbContextLib.Migrations
modelBuilder.Entity("Entities.BookEntity", b =>
{
b.Property<int>("ID")
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
@ -30,14 +31,55 @@ namespace DbContextLib.Migrations
.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.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
}
}

Binary file not shown.

@ -1,13 +1,23 @@
using System.Reflection;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 int PersonId { get; set; }
public PersonEntity? Owner { get; set; }
}
}

@ -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 int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<BookEntity> Books { get; set; } = new List<BookEntity>();
}
}

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Library.Entities;
namespace Library.DbContextLib
{
public class LibraryContext : DbContext
{
public DbSet<BookEntity> BooksSet { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=tp.Books.db");
}
}
}

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Library.Entities
{
public class BookEntity
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Isbn { get; set; }
public BookEntity(string title, string author, string isbn) {
Title = title;
Author = author;
Isbn = isbn;
}
}
}

@ -1,22 +0,0 @@
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 Library.StubbedContextLib
{
public class StubbedContext : DbContext
{
public List<BookEntity> Books { get; set; }
public StubbedContext() {
Books.Add(new BookEntity("test", "test", "test"));
Books.Add(new BookEntity("test2", "test2", "test2"));
}
}
}

@ -0,0 +1,53 @@
using System.Reflection;
using System.Text;
namespace Model
{
public class Book
{
private long id;
public long Id
{
get { return id; }
set { id = value;}
}
private string title;
public string Title
{
get { return title; }
set { title = value; }
}
private string author;
public string Author
{
get { return author; }
set { author = value; }
}
private string isbn;
public string Isbn {
get { return isbn; }
set { isbn = value; }
}
public Book(long id, string title = "", string author = "", string isbn = "")
{
Id = id;
Title = title;
Author = author;
Isbn = isbn;
}
public string Tostring(StringBuilder sb)
{
return Id.ToString() + " " + Title.ToString() + " " + Author.ToString() + " " + Isbn.ToString() ;
}
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public enum BookOrderCriteria
{
None,
ByTitle,
ByAuthor,
ByIsbn
}
}

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public interface IDataManager
{
//public IEnumerable<Book> GetBooks(int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByTitle(string title, int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByAuthor(string author, int index, int count, BookOrderCriteria orderCriterium);
//public IEnumerable<Book> GetBooksByIsbn(string isbn, int index, int count, BookOrderCriteria orderCriterium);
public Book GetBookById(long id);
//public void CreateBook(string title, string author, string isbn);
//public void UpdateBook(long id, Book book);
//public void DeleteBook(long id);
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,140 @@
using DbContextLib;
using Entities;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Model;
namespace Model2Entities
{
public class DbDataManager : IDataManager
{
public void CreateBook(Book book) {
using (var context = new LibraryContext())
{
context.Database.EnsureCreated();
if (!context.BooksSet.Contains(book.ConvertToEntity())){
context.BooksSet.Add(book.ConvertToEntity());
}
context.SaveChanges();
}
}
public List<Book> GetAllBooks()
{
List<Book> books = new List<Book>();
using (var context = new LibraryContext())
{
foreach (var bookEntity in context.BooksSet) {
books.Add(bookEntity.ConvertToModel());
}
return books;
}
}
public Book GetBookById(long id)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByAuthor(string author)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Author == author)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByTitle(string title)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Title == title)
{
return i.ConvertToModel();
}
}
return null;
}
}
public Book GetBookByIsbn(string isbn)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Isbn == isbn)
{
return i.ConvertToModel();
}
}
return null;
}
}
public void UpdateAuthor(long id, string author)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
i.Author = author;
}
}
context.SaveChanges();
}
}
public void DeleteBook(long id)
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
if (i.Id == id)
{
context.Remove(i);
}
}
context.SaveChanges();
}
}
public void DeleteAll()
{
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
{
context.Remove(i);
}
context.SaveChanges();
}
}
}
}

@ -0,0 +1,24 @@
using Entities;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Model2Entities
{
public static class Extensions
{
public static Book ConvertToModel(this BookEntity book)
{
return new Book(book.Id, book.Title, book.Author, book.Isbn);
}
public static BookEntity ConvertToEntity(this Book book)
{
return new BookEntity() {Id= book.Id, Title = book.Title, Author = book.Author, Isbn = book.Isbn };
}
}
}

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,115 @@
// <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("20240215195621_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");
b.HasData(
new
{
Id = 1L,
Author = "test",
Isbn = "test",
PersonId = 1,
Title = "test"
},
new
{
Id = 2L,
Author = "test2",
Isbn = "test2",
PersonId = 1,
Title = "test2"
});
});
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");
b.HasData(
new
{
Id = 1,
FirstName = "coco",
LastName = "test"
});
});
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,81 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
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.InsertData(
table: "BooksSet",
columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" },
values: new object[,]
{
{ 1L, "test", "test", null, 1, "test" },
{ 2L, "test2", "test2", null, 1, "test2" }
});
migrationBuilder.InsertData(
table: "PersonSet",
columns: new[] { "Id", "FirstName", "LastName" },
values: new object[] { 1, "coco", "test" });
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");
}
}
}

@ -0,0 +1,112 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using StubbedContextLib;
#nullable disable
namespace StubbedContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
partial class StubbedContextModelSnapshot : 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<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");
b.HasData(
new
{
Id = 1L,
Author = "test",
Isbn = "test",
PersonId = 1,
Title = "test"
},
new
{
Id = 2L,
Author = "test2",
Isbn = "test2",
PersonId = 1,
Title = "test2"
});
});
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");
b.HasData(
new
{
Id = 1,
FirstName = "coco",
LastName = "test"
});
});
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,11 +1,40 @@
using Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DbContextLib;
using Entities;
using Microsoft.EntityFrameworkCore;
namespace StubbedContextLib
{
public class StubbedContext : DbContext
public class StubbedContext : LibraryContext
{
public StubbedContext() {
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PersonEntity>().HasData(
new PersonEntity() { FirstName = "coco", LastName = "test", Id = 1 }
);
modelBuilder.Entity<BookEntity>().HasData(
new BookEntity() { Title = "test", Author = "test", Isbn = "test" , Id = 1, PersonId = 1},
new BookEntity() { Title = "test2", Author = "test2", Isbn = "test2",Id = 2, PersonId = 1 }
);
}
public StubbedContext(DbContextOptions<LibraryContext> options)
: base(options)
{ }
}
}

@ -19,9 +19,9 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,70 @@
using Model;
using Entities;
using Model2Entities;
using Microsoft.Extensions.DependencyModel;
using DbContextLib;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
DbDataManager dbDataManager = new DbDataManager();
dbDataManager.CreateBook(new Book(1, "mistake", "test1", "test1"));
dbDataManager.CreateBook(new Book(2, "the100", "test2", "test2"));
dbDataManager.CreateBook(new Book(3, "GOT", "lastTest", "lastTest"));
Console.WriteLine("test de récupération de tout les livres");
List<Book> books = dbDataManager.GetAllBooks();
foreach (var book in books)
{
Console.WriteLine("Titre du livre : " + book.Title);
}
Console.WriteLine("\ntest de récupération de livre par ID");
Book b1 = dbDataManager.GetBookById(3);
if (b1 != null)
{
Console.WriteLine("Le livre : " + b1.Title);
}
Console.WriteLine("\ntest de récupération de livre par Autheur");
Book b2 = dbDataManager.GetBookByIsbn("test2");
Console.WriteLine("Le livre : " + b2.Title);
Console.WriteLine("\ntest de récupération de livre par Titre");
Book b3 = dbDataManager.GetBookByTitle("the100");
Console.WriteLine("Le livre : " + b3.Title);
Console.WriteLine("\ntest de récupération de livre par ISBn");
Book b4 = dbDataManager.GetBookByAuthor("lastTest");
Console.WriteLine("Le livre : " + b4.Title);
Console.WriteLine("\n MAJ de l'auteur du livre th100");
dbDataManager.UpdateAuthor(2, "Kass Morgan");
Book the100 = dbDataManager.GetBookByAuthor("Kass Morgan");
Console.WriteLine(the100.Title + " " + the100.Author);
Console.WriteLine("\nsupresion du livre ou id = 1");
dbDataManager.DeleteBook(1);
Book test = dbDataManager.GetBookById(1);
if (test == null)
{
Console.WriteLine("livre supprimée");
}
Console.WriteLine("\nsuppression de tous les livres");
dbDataManager.DeleteAll();

@ -1,22 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<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>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Model2Entities\Model2Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestStub
{
public class BookEntity
{
public int ID { get; set; }
public string Title { get; set; }
public string Author { get; set; }
public string Isbn { get; set; }
public BookEntity(string title, string author, string isbn) {
Title = title;
Author = author;
Isbn = isbn;
}
}
}

@ -1,22 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Library.Entities;
namespace TestStub
{
public class LibraryContext : DbContext
{
public DbSet<BookEntity> BooksSet { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=tp.Books.db");
}
}
}

@ -1,47 +0,0 @@
// <auto-generated />
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
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("TestStub.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 TestStub.Migrations
{
/// <inheritdoc />
public partial class BooksMigrations : 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 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<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 +1,46 @@
using TestStub;
using StubbedContextLib;
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())
using (var context = new StubbedContext())
{
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.Database.EnsureCreated();
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"};
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())
using (var context = new StubbedContext())
{
foreach (var n in context.BooksSet)
{
Console.WriteLine($"Books: {n.ID} - {n.Title}");
Console.WriteLine($"Books: {n.Id} - {n.Title}");
}
context.SaveChanges();
}
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("t")).First();
Console.WriteLine($"{eBooks.Title} (made by {eBooks.Author})");
@ -38,12 +48,91 @@ using (var context = new LibraryContext())
context.SaveChanges();
}
using (var context = new LibraryContext())
using (var context = new StubbedContext())
{
Console.WriteLine("Deletes one item from de database");
var impostor = context.BooksSet
.SingleOrDefault(n => n.Title.Equals("mistake"));
context.Remove(impostor);
context.PersonSet.Include(pers => pers.Books).ToList();
context.SaveChanges();
}
using (var context = new StubbedContext())
{
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 StubbedContext())
{
var books = context.BooksSet;
context.PersonSet.Include(pers => pers.Books).ToList();
var person = context.PersonSet.Where(b => b.FirstName.StartsWith("E")).First();
foreach (var book in books)
{
if (book.Owner == null)
{
person.Books.Add(book);
break;
}
}
context.SaveChanges();
}
//Rendu
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
var books = context.BooksSet;
if (books != null)
{
foreach (var book in books)
{
//Console.WriteLine(book.Owner.FirstName);
if (book.Owner != null)
{
Console.WriteLine($" \n propriétaire du livre {book.Title} avant rendu : {book.Owner.FirstName}");
book.Owner = null;
Console.WriteLine($" \n propriétaire a rendu le libre {book.Title} ");
}
else
{
Console.WriteLine("\n livre sans propriétaire : " + book.Title);
}
}
}
context.SaveChanges();
}
//DeleteAllItem
using (var context = new StubbedContext())
{
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();
}

@ -1,22 +0,0 @@
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<BookEntity> Books { get; set; }
public StubbedContext() {
Books.Add(new BookEntity("test", "test", "test"));
Books.Add(new BookEntity("test2", "test2", "test2"));
}
}
}

@ -22,7 +22,9 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Library\Library.csproj" />
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
</Project>

Binary file not shown.

@ -0,0 +1 @@
global using Xunit;

@ -0,0 +1,353 @@
using DbContextLib;
using StubbedContextLib;
using Entities;
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Xunit;
using Microsoft.Data.Sqlite;
using System;
using static System.Reflection.Metadata.BlobBuilder;
namespace UnitTests
{
public class UnitTest1
{
[Fact]
public void Add_TestBooks()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList();
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.AddBook(the100);
context.AddBook(princeOfPersia);
context.AddBook(PercyJackson);
context.SaveChanges();
}
//uses another instance of the context to do the tests
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext(options))
{
context.Database.EnsureCreated();
Assert.Equal(3, context.BooksSet.Count());
Assert.Equal(1, context.BooksSet.Where(b => b.Title.Contains("the100")).Count());
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Add_TestPersons()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
//context.Database.OpenConnection();
context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList();
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
context.AddPerson(p1);
context.AddPerson(p2);
context.SaveChanges();
}
//uses another instance of the context to do the tests
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
Assert.Equal(2, context.PersonSet.Count());
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Jean")).Count());
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Modify_TestBook()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList();
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.AddBook(the100);
context.AddBook(princeOfPersia);
context.AddBook(PercyJackson);
context.SaveChanges();
}
//prepares the database with one instance of the context
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList();
var book = context.BooksSet.Where(n => n.Title.Contains("princeOfPersia")).FirstOrDefault();
book.Title = "l'Odyssée";
Assert.Equal("l'Odyssée", book.Title);
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Modify_TestPerson()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
//prepares the database with one instance of the context
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
context.AddPerson(p1);
context.AddPerson(p2);
context.SaveChanges();
}
//prepares the database with one instance of the context
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
string nameToFind = "Jean";
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).Count());
var person = context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).First();
person.FirstName = "Jacques";
Assert.Equal("Jacques", person.FirstName);
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Delete_TestPerson()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
context.AddPerson(p1);
context.AddPerson(p2);
context.SaveChanges();
}
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
var Persons = context.PersonSet;
if (Persons.Count() != 0)
{
foreach (var person in Persons)
{
context.PersonSet.Remove(person);
}
}
context.SaveChanges();
Assert.Equal( 0, context.PersonSet.Count());
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Delete_TestBook()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext())
{
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.AddBook(the100);
context.AddBook(princeOfPersia);
context.AddBook(PercyJackson);
context.SaveChanges();
}
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
var Books = context.BooksSet;
if (Books.Count() != 0)
{
foreach (var book in Books)
{
context.BooksSet.Remove(book);
}
}
context.SaveChanges();
Assert.Equal(0, context.BooksSet.Count());
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Update_TestEmprunt()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
context.AddPerson(p1);
context.AddPerson(p2);
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.AddBook(the100);
context.AddBook(princeOfPersia);
context.AddBook(PercyJackson);
context.SaveChanges();
}
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
var books = context.BooksSet;
context.PersonSet.Include(pers => pers.Books).ToList();
var person = context.PersonSet.Where(b => b.FirstName.Contains("Jean")).First();
foreach (var book in books)
{
if (book.Owner == null)
{
book.Owner = person;
break;
}
}
context.SaveChanges();
}
using (var context = new StubbedContext())
{
Assert.NotNull(context.BooksSet.Where(b => b.Owner.FirstName.Contains("Jean")).First());
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Update_TestRendu()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
context.AddPerson(p1);
context.AddPerson(p2);
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.AddBook(the100);
context.AddBook(princeOfPersia);
context.AddBook(PercyJackson);
context.SaveChanges();
}
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
var books = context.BooksSet;
context.PersonSet.Include(pers => pers.Books).ToList();
var person = context.PersonSet.Where(b => b.FirstName.StartsWith("Jean")).First();
var testbook = new BookEntity();
foreach (var book in books)
{
if (book.Owner == null)
{
book.Owner = person;
break;
}
}
foreach (var book in books)
{
//Console.WriteLine(book.Owner.FirstName);
if (book.Owner != null)
{
book.Owner = null;
}
}
Assert.Equal(context.BooksSet.Count(), context.BooksSet.Where(b => b.Owner == null).Count());
context.RemoveAll();
context.SaveChanges();
}
}
}
}

@ -0,0 +1,41 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</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>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
</Project>

@ -3,9 +3,21 @@ 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}") = "TestStub", "TestStub\TestStub.csproj", "{8960D74C-259D-4779-80B6-789BA257D2B2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestStub", "TestStub\TestStub.csproj", "{8960D74C-259D-4779-80B6-789BA257D2B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library", "Library\Library.csproj", "{CDD5A87A-187A-4BC1-9E23-5559A2211B55}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{A4130190-8883-42DE-89CA-3DF205DE710A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F793C071-E178-48D3-BBE6-22F99D102C2B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{5C673F26-2E43-4AA8-944E-1A3B309927CF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\UnitTests.csproj", "{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{69CD70C4-A73C-4C22-864D-B72204B7AD9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model2Entities", "Model2Entities\Model2Entities.csproj", "{B5E6450F-A53F-4986-9740-A648E4E823EC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestModel2Entities", "TestModel2Entities\TestModel2Entities.csproj", "{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -17,10 +29,34 @@ Global
{8960D74C-259D-4779-80B6-789BA257D2B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8960D74C-259D-4779-80B6-789BA257D2B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8960D74C-259D-4779-80B6-789BA257D2B2}.Release|Any CPU.Build.0 = Release|Any CPU
{CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Release|Any CPU.Build.0 = Release|Any CPU
{A4130190-8883-42DE-89CA-3DF205DE710A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4130190-8883-42DE-89CA-3DF205DE710A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4130190-8883-42DE-89CA-3DF205DE710A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4130190-8883-42DE-89CA-3DF205DE710A}.Release|Any CPU.Build.0 = Release|Any CPU
{F793C071-E178-48D3-BBE6-22F99D102C2B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F793C071-E178-48D3-BBE6-22F99D102C2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F793C071-E178-48D3-BBE6-22F99D102C2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F793C071-E178-48D3-BBE6-22F99D102C2B}.Release|Any CPU.Build.0 = Release|Any CPU
{5C673F26-2E43-4AA8-944E-1A3B309927CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C673F26-2E43-4AA8-944E-1A3B309927CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C673F26-2E43-4AA8-944E-1A3B309927CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C673F26-2E43-4AA8-944E-1A3B309927CF}.Release|Any CPU.Build.0 = Release|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73EC457C-A959-47DD-A4B5-F836DE8CFBAD}.Release|Any CPU.Build.0 = Release|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69CD70C4-A73C-4C22-864D-B72204B7AD9D}.Release|Any CPU.Build.0 = Release|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B5E6450F-A53F-4986-9740-A648E4E823EC}.Release|Any CPU.Build.0 = Release|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A60AD88-4D1C-40B5-8E31-9B5CAAD2D48E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save