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-23CS6SP\SQLEXPRESS;Initial Catalog=CanteenDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasOne(dp => dp.Dish) .WithMany(d => d.Products) .HasForeignKey(dp => dp.DishId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(dp => dp.Product) .WithMany(p => p.Dishes) .HasForeignKey(dp => dp.ProductId) .OnDelete(DeleteBehavior.Restrict); //===================================== modelBuilder.Entity() .HasOne(dp => dp.Lunch) .WithMany(d => d.Orders) .HasForeignKey(dp => dp.LunchId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(dp => dp.Order) .WithMany(p => p.Lunches) .HasForeignKey(dp => dp.OrderId) .OnDelete(DeleteBehavior.Restrict); //===================================== modelBuilder.Entity() .HasOne(dp => dp.Lunch) .WithMany(d => d.Products) .HasForeignKey(dp => dp.LunchId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(dp => dp.Product) .WithMany(p => p.Lunches) .HasForeignKey(dp => dp.ProductId) .OnDelete(DeleteBehavior.Restrict); //===================================== modelBuilder.Entity() .HasOne(dp => dp.Order) .WithMany(d => d.Cooks) .HasForeignKey(dp => dp.OrderId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(dp => dp.Cook) .WithMany(p => p.Orders) .HasForeignKey(dp => dp.CookId) .OnDelete(DeleteBehavior.Restrict); //===================================== modelBuilder.Entity() .HasOne(dp => dp.Order) .WithMany(d => d.Tablewares) .HasForeignKey(dp => dp.OrderId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(dp => dp.Tableware) .WithMany(p => p.Orders) .HasForeignKey(dp => dp.TablewareId) .OnDelete(DeleteBehavior.Restrict); //===================================== modelBuilder.Entity() .HasOne(dp => dp.Product) .WithMany(d => d.Cooks) .HasForeignKey(dp => dp.ProductId) .OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity() .HasOne(dp => dp.Cook) .WithMany(p => p.Products) .HasForeignKey(dp => dp.CookId) .OnDelete(DeleteBehavior.Restrict); //===================================== modelBuilder.Entity() .HasOne(b => b.Visitor) .WithMany(a => a.Orders) .OnDelete(DeleteBehavior.Cascade); //===================================== modelBuilder.Entity() .HasOne(b => b.Visitor) .WithMany(a => a.Lunches) .OnDelete(DeleteBehavior.Cascade); //===================================== modelBuilder.Entity() .HasOne(b => b.Visitor) .WithMany(a => a.Orders) .OnDelete(DeleteBehavior.Cascade); //===================================== modelBuilder.Entity() .HasOne(b => b.Visitor) .WithMany(a => a.Orders) .OnDelete(DeleteBehavior.Cascade); //===================================== } public virtual DbSet Cooks { set; get; } public virtual DbSet OrderCook { set; get; } public virtual DbSet Dishes { set; get; } public virtual DbSet Lunches { set; get; } public virtual DbSet LunchOrder { set; get; } public virtual DbSet Managers { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Products { set; get; } public virtual DbSet ProductCook { set; get; } public virtual DbSet LunchProduct { set; get; } public virtual DbSet OrderTableware { set; get; } public virtual DbSet DishProduct { set; get; } public virtual DbSet Tablewares { set; get; } public virtual DbSet Visitors { set; get; } } }