ajouts migrations fonctionnelles

part1
Tom RAMBEAU 1 year ago
parent 5ba4977cae
commit 90b016f200

@ -0,0 +1,47 @@
// <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
}
}
}

@ -0,0 +1,36 @@
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,44 @@
// <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
}
}
}

Binary file not shown.

@ -15,7 +15,7 @@ namespace Library.DbContextLib
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite($"Data Source=tp1.Books.db");
optionsBuilder.UseSqlite("Data Source=tp.Books.db");
}
}

Binary file not shown.

@ -0,0 +1,23 @@
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;
}
}
}

@ -0,0 +1,22 @@
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");
}
}
}

@ -0,0 +1,47 @@
// <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
}
}
}

@ -0,0 +1,36 @@
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");
}
}
}

@ -0,0 +1,44 @@
// <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,6 +1,4 @@
using Library;
using Library.DbContextLib;
using Library.Entities;
using TestStub;
using Microsoft.Extensions.Options;
// See https://aka.ms/new-console-template for more information
@ -8,13 +6,16 @@ Console.WriteLine("Hello, World!");
using (var context = new LibraryContext())
{
BookEntity chewie = new BookEntity("B3", "test", "test2");
BookEntity yoda = new BookEntity ("B4", "test", "test2");
BookEntity ewok = new BookEntity ("C5", "test", "test3");
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.SaveChanges();
}
@ -23,18 +24,26 @@ using (var context = new LibraryContext())
{
foreach (var n in context.BooksSet)
{
Console.WriteLine($"{n.ID} - {n.Title}");
Console.WriteLine($"Books: {n.ID} - {n.Title}");
}
context.SaveChanges();
}
using (var context = new LibraryContext())
{
var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("B")).First();
Console.WriteLine($"{eBooks.Title} (born in {eBooks.Author})");
var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("t")).First();
Console.WriteLine($"{eBooks.Title} (made by {eBooks.Author})");
eBooks.Title = "Border";
context.SaveChanges();
}
eBooks.Title = "Wicket";
using (var context = new LibraryContext())
{
Console.WriteLine("Deletes one item from de database");
var impostor = context.BooksSet
.SingleOrDefault(n => n.Title.Equals("mistake"));
context.Remove(impostor);
context.SaveChanges();
}

@ -0,0 +1,22 @@
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"));
}
}
}

Binary file not shown.

@ -1,7 +0,0 @@
namespace TestStubEF
{
public class Class1
{
}
}

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

@ -1,4 +0,0 @@
using Microsoft.EntityFrameworkCore.Design;
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

@ -1,29 +0,0 @@
<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.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.SqlServer" 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="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
</Project>
Loading…
Cancel
Save