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-AOSK1F5\SQLEXPRESS;Initial Catalog=CanteenDataBase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() .HasKey(dp => new { dp.DishId, dp.ProductId }); modelBuilder.Entity() .HasOne(dp => dp.Dish) .WithMany(d => d.Products) .HasForeignKey(dp => dp.DishId) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity() .HasOne(dp => dp.Product) .WithMany(p => p.Dishes) .HasForeignKey(dp => dp.ProductId) .OnDelete(DeleteBehavior.NoAction); //===================================== modelBuilder.Entity() .HasKey(dp => new { dp.LunchId, dp.OrderId }); modelBuilder.Entity() .HasOne(dp => dp.Lunch) .WithMany(d => d.Orders) .HasForeignKey(dp => dp.LunchId) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity() .HasOne(dp => dp.Order) .WithMany(p => p.Lunches) .HasForeignKey(dp => dp.OrderId) .OnDelete(DeleteBehavior.NoAction); //===================================== modelBuilder.Entity() .HasKey(dp => new { dp.LunchId, dp.ProductId }); modelBuilder.Entity() .HasOne(dp => dp.Lunch) .WithMany(d => d.Products) .HasForeignKey(dp => dp.LunchId) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity() .HasOne(dp => dp.Product) .WithMany(p => p.Lunches) .HasForeignKey(dp => dp.ProductId) .OnDelete(DeleteBehavior.NoAction); //===================================== modelBuilder.Entity() .HasKey(dp => new { dp.OrderId, dp.CookId }); modelBuilder.Entity() .HasOne(dp => dp.Order) .WithMany(d => d.Cooks) .HasForeignKey(dp => dp.OrderId) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity() .HasOne(dp => dp.Cook) .WithMany(p => p.Orders) .HasForeignKey(dp => dp.CookId) .OnDelete(DeleteBehavior.NoAction); //===================================== modelBuilder.Entity() .HasKey(dp => new { dp.OrderId, dp.DishId }); modelBuilder.Entity() .HasOne(dp => dp.Order) .WithMany(d => d.Dishes) .HasForeignKey(dp => dp.OrderId) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity() .HasOne(dp => dp.Dish) .WithMany(p => p.Orders) .HasForeignKey(dp => dp.DishId) .OnDelete(DeleteBehavior.NoAction); //===================================== modelBuilder.Entity() .HasKey(dp => new { dp.ProductId, dp.CookId }); modelBuilder.Entity() .HasOne(dp => dp.Product) .WithMany(d => d.Cooks) .HasForeignKey(dp => dp.ProductId) .OnDelete(DeleteBehavior.NoAction); modelBuilder.Entity() .HasOne(dp => dp.Cook) .WithMany(p => p.Products) .HasForeignKey(dp => dp.CookId) .OnDelete(DeleteBehavior.NoAction); //===================================== modelBuilder.Entity() .HasOne(b => b.Visitor) .WithMany(a => a.Orders) .OnDelete(DeleteBehavior.NoAction); //===================================== } 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 OrderDish { set; get; } public virtual DbSet DishProduct { set; get; } public virtual DbSet Tablewares { set; get; } public virtual DbSet Visitors { set; get; } } }