Загрузка данных


using System;
using System.Collections.Generic;

namespace LibrarySystem
{
    public class Author
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ICollection<Book> Books { get; set; }
    }

    public class Book
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public decimal Price { get; set; }
        public int Pages { get; set; }
        public int AuthorId { get; set; }
        public Author Author { get; set; }
        public ICollection<Loan> Loans { get; set; }
    }

    public class Reader
    {
        public int Id { get; set; }
        public string FullName { get; set; }
        public string CardNumber { get; set; }
        public ICollection<Loan> Loans { get; set; }
    }

    public class Loan
    {
        public int Id { get; set; }
        public int BookId { get; set; }
        public int ReaderId { get; set; }
        public DateTime LoanDate { get; set; }
        public DateTime? ReturnDate { get; set; }
        public Book Book { get; set; }
        public Reader Reader { get; set; }
    }

// КОНТЕКСТ С НАСТРОЙКАМИ FLUENT API
public class LibraryContext : DbContext
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }
    public DbSet<Reader> Readers { get; set; }
    public DbSet<Loan> Loans { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        string connStr = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=MultiTableLibrary;Integrated Security=True;";
        options.UseSqlServer(connStr);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Author
        modelBuilder.Entity<Author>(entity =>
        {
            entity.ToTable("Authors");
            entity.HasKey(a => a.Id);
            entity.Property(a => a.FirstName).IsRequired().HasMaxLength(100);
            entity.Property(a => a.LastName).IsRequired().HasMaxLength(100);
        });

        // Book
        modelBuilder.Entity<Book>(entity =>
        {
            entity.ToTable("Books");
            entity.HasKey(b => b.Id);
            entity.Property(b => b.Title).IsRequired().HasMaxLength(200);
            entity.Property(b => b.Price).HasColumnType("decimal(10,2)");
            entity.Property(b => b.Pages);

            // Связь 1:N c Author
            entity.HasOne(b => b.Author)
                .WithMany(a => a.Books)
                .HasForeignKey(b => b.AuthorId)
                .OnDelete(DeleteBehavior.Cascade);
        });
    }
}


// Reader
modelBuilder.Entity<Reader>(entity =>
{
    entity.ToTable("Readers");
    entity.HasKey(r => r.Id);
    entity.Property(r => r.FullName).IsRequired().HasMaxLength(150);
    entity.Property(r => r.CardNumber).IsRequired().HasMaxLength(20);
    entity.HasIndex(r => r.CardNumber).IsUnique();
});

// Loan
modelBuilder.Entity<Loan>(entity =>
{
    entity.ToTable("Loans");
    entity.HasKey(l => l.Id);

    entity.Property(l => l.LoanDate)
        .HasColumnType("datetime2")
        .HasDefaultValueSql("GETDATE()");

    entity.Property(l => l.ReturnDate).HasColumnType("datetime2");

    // Связь с Book
    entity.HasOne(l => l.Book)
        .WithMany(b => b.Loans)
        .HasForeignKey(l => l.BookId);

    // Связь с Reader
    entity.HasOne(l => l.Reader)
        .WithMany(r => r.Loans)
        .HasForeignKey(l => l.ReaderId);
});

// SEED DATA (начальные данные)
modelBuilder.Entity<Author>().HasData(
    new Author { Id = 1, FirstName = "Роджер", LastName = "Желязны" },
    new Author { Id = 2, FirstName = "Айзек", LastName = "Азимов" }
);