Y2S1-Web_Apps_and_Services/ThAmCo.Catering/Data/CateringDbContext.cs

59 lines
1.8 KiB
C#
Raw Permalink Normal View History

2020-06-07 21:36:12 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using ThAmCo.Catering.Data;
namespace ThAmCo.Venues.Data
{
public class CateringDbContext : DbContext
{
public DbSet<Menu> Menus { get; set; }
public DbSet<FoodBooking> FoodBookings { get; set; }
private readonly IHostingEnvironment _hostEnv;
public CateringDbContext(DbContextOptions<CateringDbContext> options,
IHostingEnvironment env) : base(options)
{
_hostEnv = env;
}
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
base.OnConfiguring(builder);
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.HasDefaultSchema("thamco.catering");
builder.Entity<Menu>()
.HasKey(m => m.Id);
builder.Entity<FoodBooking>()
.HasKey(f => f.Id);
builder.Entity<Menu>()
.HasMany(f => f.FoodBookings)
.WithOne(m => m.Menu)
.HasForeignKey(m => m.Id);
if (_hostEnv != null && _hostEnv.IsDevelopment())
{
builder.Entity<Menu>()
.HasData(
new Menu { Id = 1, Price = 8.50, Items = "Chicken Pate, Chicken Roast, Chicken Cake" },
new Menu { Id = 2, Price = 7.50, Items = "Toast, Cheese On Toast, Jam on Toast" },
new Menu { Id = 3, Price = 9, Items = "Mac and Cheese, Alan's Special, Cheesecake" }
);
}
}
}
}