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
2020-12-17 16:52:43 +01:00

39 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}
}