This repository has been archived on 2021-04-30. You can view files and clone it, but cannot push or open issues or pull requests.
blazing-workshop/BlazingPizza.Server/PizzaStoreContext.cs

39 lines
1.5 KiB
C#
Raw Normal View History

2020-12-17 15:52:43 +00:00
using IdentityServer4.EntityFramework.Options;
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace BlazingPizza.Server
{
public class PizzaStoreContext : ApiAuthorizationDbContext<PizzaStoreUser>
{
public PizzaStoreContext(
DbContextOptions options,
IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
{
}
public DbSet<Order> Orders { get; set; }
public DbSet<Pizza> Pizzas { get; set; }
public DbSet<PizzaSpecial> Specials { get; set; }
public DbSet<Topping> Toppings { get; set; }
public DbSet<NotificationSubscription> NotificationSubscriptions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configuring a many-to-many special -> topping relationship that is friendly for serialization
modelBuilder.Entity<PizzaTopping>().HasKey(pst => new { pst.PizzaId, pst.ToppingId });
modelBuilder.Entity<PizzaTopping>().HasOne<Pizza>().WithMany(ps => ps.Toppings);
modelBuilder.Entity<PizzaTopping>().HasOne(pst => pst.Topping).WithMany();
// Inline the Lat-Long pairs in Order rather than having a FK to another table
modelBuilder.Entity<Order>().OwnsOne(o => o.DeliveryLocation);
}
}
}