[b66b3ac] | 1 | using backend.Entities;
|
---|
| 2 | using Microsoft.EntityFrameworkCore;
|
---|
| 3 |
|
---|
| 4 | namespace backend.Data
|
---|
| 5 | {
|
---|
| 6 | public class DataContext : DbContext
|
---|
| 7 | {
|
---|
| 8 | public DataContext(DbContextOptions<DataContext> dbContextOptions) : base(dbContextOptions)
|
---|
| 9 | {
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | private DbSet<User> users;
|
---|
| 13 | public DbSet<User> Users
|
---|
| 14 | {
|
---|
| 15 | get
|
---|
| 16 | {
|
---|
| 17 | if (users == null)
|
---|
| 18 | {
|
---|
| 19 | users = Set<User>();
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | return users;
|
---|
| 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | private DbSet<Restaurant> restaurants;
|
---|
| 27 | public DbSet<Restaurant> Restoraunts
|
---|
| 28 | {
|
---|
| 29 | get
|
---|
| 30 | {
|
---|
| 31 | if (restaurants == null)
|
---|
| 32 | {
|
---|
| 33 | restaurants = Set<Restaurant>();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | return restaurants;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | private DbSet<Reservation> reservations;
|
---|
| 41 | public DbSet<Reservation> Reservations
|
---|
| 42 | {
|
---|
| 43 | get
|
---|
| 44 | {
|
---|
| 45 | if (reservations == null)
|
---|
| 46 | {
|
---|
| 47 | reservations = Set<Reservation>();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return reservations;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[cc4db18] | 54 | private DbSet<MenuItem> menuItems;
|
---|
| 55 | public DbSet<MenuItem> MenuItems
|
---|
| 56 | {
|
---|
| 57 | get
|
---|
| 58 | {
|
---|
| 59 | if (menuItems == null)
|
---|
| 60 | {
|
---|
| 61 | menuItems = Set<MenuItem>();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | return menuItems;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[b66b3ac] | 68 |
|
---|
| 69 | protected override void OnModelCreating(ModelBuilder modelBuilder)
|
---|
| 70 | {
|
---|
| 71 | //
|
---|
| 72 | // User
|
---|
| 73 | //
|
---|
| 74 | modelBuilder.Entity<User>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
| 75 | modelBuilder.Entity<User>()
|
---|
| 76 | .HasOne(p => p.Restaurant)
|
---|
| 77 | .WithOne(b => b.Owner);
|
---|
| 78 |
|
---|
| 79 | //
|
---|
| 80 | // Restoraunt
|
---|
| 81 | //
|
---|
| 82 | modelBuilder.Entity<Restaurant>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
| 83 | modelBuilder.Entity<Restaurant>()
|
---|
| 84 | .HasOne(p => p.Owner)
|
---|
| 85 | .WithOne(b => b.Restaurant)
|
---|
| 86 | .HasForeignKey<Restaurant>(k => k.OwnerFk);
|
---|
| 87 | modelBuilder.Entity<Restaurant>()
|
---|
| 88 | .HasMany(p => p.Reservations)
|
---|
| 89 | .WithOne(b => b.Restaurant);
|
---|
[cc4db18] | 90 | modelBuilder.Entity<Restaurant>()
|
---|
| 91 | .HasMany(p => p.Menu)
|
---|
| 92 | .WithOne(b => b.Restaurant);
|
---|
| 93 |
|
---|
[b66b3ac] | 94 |
|
---|
| 95 | //
|
---|
| 96 | // Reservation
|
---|
| 97 | //
|
---|
| 98 | modelBuilder.Entity<Reservation>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
| 99 | modelBuilder.Entity<Reservation>()
|
---|
| 100 | .HasOne(p => p.Restaurant)
|
---|
| 101 | .WithMany(b => b.Reservations);
|
---|
[cc4db18] | 102 |
|
---|
| 103 | //
|
---|
| 104 | // MenuItem
|
---|
| 105 | //
|
---|
| 106 | modelBuilder.Entity<MenuItem>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
| 107 | modelBuilder.Entity<MenuItem>()
|
---|
| 108 | .HasOne(p => p.Restaurant)
|
---|
| 109 | .WithMany(b => b.Menu);
|
---|
[b66b3ac] | 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|