source: resTools_backend/backend/Data/DataContext.cs@ cc4db18

Last change on this file since cc4db18 was cc4db18, checked in by Danilo <danilo.najkov@…>, 2 years ago

reservation module changes + contact module + menu module

  • Property mode set to 100644
File size: 2.9 KB
Line 
1using backend.Entities;
2using Microsoft.EntityFrameworkCore;
3
4namespace 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
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
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);
90 modelBuilder.Entity<Restaurant>()
91 .HasMany(p => p.Menu)
92 .WithOne(b => b.Restaurant);
93
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);
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);
110 }
111 }
112}
Note: See TracBrowser for help on using the repository browser.