mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2025-12-06 05:32:03 +01:00
79 lines
2.5 KiB
C#
79 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
|
|
namespace Packt.Shared
|
|
{
|
|
public partial class NorthwindContext : DbContext
|
|
{
|
|
public NorthwindContext()
|
|
{
|
|
}
|
|
|
|
public NorthwindContext(DbContextOptions<NorthwindContext> options)
|
|
: base(options)
|
|
{
|
|
}
|
|
|
|
public virtual DbSet<Category> Categories { get; set; } = null!;
|
|
public virtual DbSet<Customer> Customers { get; set; } = null!;
|
|
public virtual DbSet<Employee> Employees { get; set; } = null!;
|
|
public virtual DbSet<EmployeeTerritory> EmployeeTerritories { get; set; } = null!;
|
|
public virtual DbSet<Order> Orders { get; set; } = null!;
|
|
public virtual DbSet<OrderDetail> OrderDetails { get; set; } = null!;
|
|
public virtual DbSet<Product> Products { get; set; } = null!;
|
|
public virtual DbSet<Shipper> Shippers { get; set; } = null!;
|
|
public virtual DbSet<Supplier> Suppliers { get; set; } = null!;
|
|
public virtual DbSet<Territory> Territories { get; set; } = null!;
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
string dir = Environment.CurrentDirectory;
|
|
string path = string.Empty;
|
|
|
|
if (dir.EndsWith("net7.0"))
|
|
{
|
|
// Running in the <project>\bin\<Debug|Release>\net7.0 directory.
|
|
path = Path.Combine("..", "..", "..", "..", "Northwind.db");
|
|
}
|
|
else
|
|
{
|
|
// Running in the <project> directory.
|
|
path = Path.Combine("..", "Northwind.db");
|
|
}
|
|
|
|
optionsBuilder.UseSqlite($"Filename={path}");
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<OrderDetail>(entity =>
|
|
{
|
|
entity.HasKey(e => new { e.OrderId, e.ProductId });
|
|
|
|
entity.HasOne(d => d.Order)
|
|
.WithMany(p => p.OrderDetails)
|
|
.HasForeignKey(d => d.OrderId)
|
|
.OnDelete(DeleteBehavior.ClientSetNull);
|
|
|
|
entity.HasOne(d => d.Product)
|
|
.WithMany(p => p.OrderDetails)
|
|
.HasForeignKey(d => d.ProductId)
|
|
.OnDelete(DeleteBehavior.ClientSetNull);
|
|
});
|
|
|
|
modelBuilder.Entity<Product>()
|
|
.Property(product => product.UnitPrice)
|
|
.HasConversion<double>();
|
|
|
|
OnModelCreatingPartial(modelBuilder);
|
|
}
|
|
|
|
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
|
}
|
|
}
|