Files
TP-Fluent/BookHive/BookHiveDbContext.cs
2026-03-17 10:27:53 +01:00

260 lines
8.2 KiB
C#

using BookHive.Models;
using Microsoft.EntityFrameworkCore;
namespace BookHive;
public class BookHiveDbContext : DbContext
{
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<Loan> Loans { get; set; }
public DbSet<Review> Reviews { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString =
"Server=romaric-thibault.fr;" + // Serveur SQL
"Database=mathys_BookHive;" + // Nom de la base
"User Id=mathys;" + // Utilisateur
"Password=Onto9-Cage-Afflicted;" + // Mot de passe
"TrustServerCertificate=true;"; // Accepte certificat auto-signé
optionsBuilder.UseSqlServer(connectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Review>()
.HasIndex(x => new { x.BookId, x.MemberId })
.IsUnique();
modelBuilder.Entity<Author>().HasData(
new Author
{
Id = 1,
FirstName = "George",
LastName = "Orwell",
Biography = "Auteur britannique connu pour ses romans dystopiques.",
BirthDate = new DateOnly(1903, 6, 25),
Nationality = "Britannique"
},
new Author
{
Id = 2,
FirstName = "Jane",
LastName = "Austen",
Biography = "Romancière anglaise célèbre pour ses romans sur la société.",
BirthDate = new DateOnly(1775, 12, 16),
Nationality = "Britannique"
},
new Author
{
Id = 3,
FirstName = "Victor",
LastName = "Hugo",
Biography = "Poète et écrivain français du XIXe siècle.",
BirthDate = new DateOnly(1802, 2, 26),
Nationality = "Française"
}
);
modelBuilder.Entity<Book>().HasData(
new Book
{
Id = 1,
Title = "1984",
Isbn = "9780451524935",
Summary = "Roman dystopique sur un régime totalitaire.",
PageCount = 328,
PublishedDate = new DateOnly(1949, 6, 8),
Genre = "Dystopie",
AuthorId = 1
},
new Book
{
Id = 2,
Title = "Animal Farm",
Isbn = "9780451526342",
Summary = "Satire politique sous forme de fable animale.",
PageCount = 112,
PublishedDate = new DateOnly(1945, 8, 17),
Genre = "Satire",
AuthorId = 1
},
new Book
{
Id = 3,
Title = "Pride and Prejudice",
Isbn = "9780141439518",
Summary = "Histoire romantique dans l'Angleterre du XIXe siècle.",
PageCount = 279,
PublishedDate = new DateOnly(1813, 1, 28),
Genre = "Roman",
AuthorId = 2
},
new Book
{
Id = 4,
Title = "Sense and Sensibility",
Isbn = "9780141439662",
Summary = "Roman sur les sœurs Dashwood.",
PageCount = 226,
PublishedDate = new DateOnly(1811, 10, 30),
Genre = "Roman",
AuthorId = 2
},
new Book
{
Id = 5,
Title = "Les Misérables",
Isbn = "9782070409189",
Summary = "Grande fresque sociale sur la misère et la justice.",
PageCount = 1463,
PublishedDate = new DateOnly(1862, 4, 3),
Genre = "Roman historique",
AuthorId = 3
},
new Book
{
Id = 6,
Title = "Notre-Dame de Paris",
Isbn = "9782253004226",
Summary = "Roman historique se déroulant à Paris.",
PageCount = 940,
PublishedDate = new DateOnly(1831, 1, 14),
Genre = "Roman historique",
AuthorId = 3
}
);
modelBuilder.Entity<Member>().HasData(
new Member
{
Id = 1,
Email = "alice@example.com",
FirstName = "Alice",
LastName = "Martin",
MembershipDate = new DateOnly(2023, 1, 10),
IsActive = true
},
new Member
{
Id = 2,
Email = "bob@example.com",
FirstName = "Bob",
LastName = "Durand",
MembershipDate = new DateOnly(2023, 5, 12),
IsActive = true
},
new Member
{
Id = 3,
Email = "claire@example.com",
FirstName = "Claire",
LastName = "Petit",
MembershipDate = new DateOnly(2024, 2, 2),
IsActive = true
},
new Member
{
Id = 4,
Email = "david@example.com",
FirstName = "David",
LastName = "Bernard",
MembershipDate = new DateOnly(2024, 6, 15),
IsActive = true
}
);
modelBuilder.Entity<Loan>().HasData(
new Loan
{
Id = 1,
BookId = 1,
MemberId = 1,
LoanDate = new DateOnly(2025, 12, 1),
DueDate = new DateOnly(2025, 12, 15),
ReturnDate = new DateOnly(2025, 12, 10)
},
new Loan
{
Id = 2,
BookId = 2,
MemberId = 2,
LoanDate = new DateOnly(2025, 12, 5),
DueDate = new DateOnly(2025, 12, 20),
ReturnDate = new DateOnly(2025, 12, 18)
},
new Loan
{
Id = 3,
BookId = 3,
MemberId = 3,
LoanDate = new DateOnly(2025, 12, 10),
DueDate = new DateOnly(2025, 12, 25),
ReturnDate = null
},
new Loan
{
Id = 4,
BookId = 4,
MemberId = 1,
LoanDate = new DateOnly(2025, 12, 12),
DueDate = new DateOnly(2025, 12, 27),
ReturnDate = null
},
new Loan
{
Id = 5,
BookId = 5,
MemberId = 2,
LoanDate = new DateOnly(2025, 11, 1),
DueDate = new DateOnly(2025, 11, 15),
ReturnDate = new DateOnly(2025, 11, 14)
}
);
modelBuilder.Entity<Review>().HasData(
new Review
{
Id = 1,
BookId = 1,
MemberId = 1,
Rating = 5,
Comment = "Un chef-d'œuvre dystopique.",
CreatedAt = new DateTime(2025, 12, 1)
},
new Review
{
Id = 2,
BookId = 3,
MemberId = 2,
Rating = 4,
Comment = "Très bon roman classique.",
CreatedAt = new DateTime(2025, 12, 2)
},
new Review
{
Id = 3,
BookId = 5,
MemberId = 3,
Rating = 5,
Comment = "Incroyable roman historique.",
CreatedAt = new DateTime(2025, 12, 3)
},
new Review
{
Id = 4,
BookId = 2,
MemberId = 4,
Rating = 4,
Comment = "Drôle et intelligent.",
CreatedAt = new DateTime(2025, 12, 4)
}
);
}
}