PIbd-21_BatylkinaAO_MusoevD.../Canteen/CanteenDatabaseImplement/CanteenDatabase.cs
2023-06-21 19:25:26 +04:00

137 lines
5.5 KiB
C#

using CanteenDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using System.Diagnostics.Contracts;
namespace CanteenDatabaseImplement
{
public class CanteenDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-A68O3K0;Initial Catalog=CanteenDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<DishProduct>()
.HasOne(dp => dp.Dish)
.WithMany(d => d.Products)
.HasForeignKey(dp => dp.DishId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<DishProduct>()
.HasOne(dp => dp.Product)
.WithMany(p => p.Dishes)
.HasForeignKey(dp => dp.ProductId)
.OnDelete(DeleteBehavior.Restrict);
//=====================================
modelBuilder.Entity<LunchOrder>()
.HasOne(dp => dp.Lunch)
.WithMany(d => d.Orders)
.HasForeignKey(dp => dp.LunchId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<LunchOrder>()
.HasOne(dp => dp.Order)
.WithMany(p => p.Lunches)
.HasForeignKey(dp => dp.OrderId)
.OnDelete(DeleteBehavior.Restrict);
//=====================================
modelBuilder.Entity<LunchProduct>()
.HasOne(dp => dp.Lunch)
.WithMany(d => d.Products)
.HasForeignKey(dp => dp.LunchId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<LunchProduct>()
.HasOne(dp => dp.Product)
.WithMany(p => p.Lunches)
.HasForeignKey(dp => dp.ProductId)
.OnDelete(DeleteBehavior.Restrict);
//=====================================
modelBuilder.Entity<OrderCook>()
.HasOne(dp => dp.Order)
.WithMany(d => d.Cooks)
.HasForeignKey(dp => dp.OrderId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<OrderCook>()
.HasOne(dp => dp.Cook)
.WithMany(p => p.Orders)
.HasForeignKey(dp => dp.CookId)
.OnDelete(DeleteBehavior.Restrict);
//=====================================
modelBuilder.Entity<OrderTableware>()
.HasOne(dp => dp.Order)
.WithMany(d => d.Tablewares)
.HasForeignKey(dp => dp.OrderId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<OrderTableware>()
.HasOne(dp => dp.Tableware)
.WithMany(p => p.Orders)
.HasForeignKey(dp => dp.TablewareId)
.OnDelete(DeleteBehavior.Restrict);
//=====================================
modelBuilder.Entity<ProductCook>()
.HasOne(dp => dp.Product)
.WithMany(d => d.Cooks)
.HasForeignKey(dp => dp.ProductId)
.OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<ProductCook>()
.HasOne(dp => dp.Cook)
.WithMany(p => p.Products)
.HasForeignKey(dp => dp.CookId)
.OnDelete(DeleteBehavior.Restrict);
//=====================================
modelBuilder.Entity<Order>()
.HasOne(b => b.Visitor)
.WithMany(a => a.Orders)
.OnDelete(DeleteBehavior.Cascade);
//=====================================
modelBuilder.Entity<Lunch>()
.HasOne(b => b.Visitor)
.WithMany(a => a.Lunches)
.OnDelete(DeleteBehavior.Cascade);
//=====================================
modelBuilder.Entity<Order>()
.HasOne(b => b.Visitor)
.WithMany(a => a.Orders)
.OnDelete(DeleteBehavior.Cascade);
//=====================================
modelBuilder.Entity<Order>()
.HasOne(b => b.Visitor)
.WithMany(a => a.Orders)
.OnDelete(DeleteBehavior.Cascade);
//=====================================
}
public virtual DbSet<Cook> Cooks { set; get; }
public virtual DbSet<OrderCook> OrderCook { set; get; }
public virtual DbSet<Dish> Dishes { set; get; }
public virtual DbSet<Lunch> Lunches { set; get; }
public virtual DbSet<LunchOrder> LunchOrder { set; get; }
public virtual DbSet<Manager> Managers { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Product> Products { set; get; }
public virtual DbSet<ProductCook> ProductCook { set; get; }
public virtual DbSet<LunchProduct> LunchProduct { set; get; }
public virtual DbSet<OrderTableware> OrderTableware { set; get; }
public virtual DbSet<DishProduct> DishProduct { set; get; }
public virtual DbSet<Tableware> Tablewares { set; get; }
public virtual DbSet<Visitor> Visitors { set; get; }
}
}