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 |
|
---|
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 | private DbSet<Review> reviews;
|
---|
69 | public DbSet<Review> Reviews
|
---|
70 | {
|
---|
71 | get
|
---|
72 | {
|
---|
73 | if (reviews == null)
|
---|
74 | {
|
---|
75 | reviews = Set<Review>();
|
---|
76 | }
|
---|
77 |
|
---|
78 | return reviews;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private DbSet<ToDoItem> toDoItems;
|
---|
83 | public DbSet<ToDoItem> ToDoItems
|
---|
84 | {
|
---|
85 | get
|
---|
86 | {
|
---|
87 | if (toDoItems == null)
|
---|
88 | {
|
---|
89 | toDoItems = Set<ToDoItem>();
|
---|
90 | }
|
---|
91 |
|
---|
92 | return toDoItems;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | protected override void OnModelCreating(ModelBuilder modelBuilder)
|
---|
98 | {
|
---|
99 | //
|
---|
100 | // User
|
---|
101 | //
|
---|
102 | modelBuilder.Entity<User>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
103 | modelBuilder.Entity<User>()
|
---|
104 | .HasOne(p => p.Restaurant)
|
---|
105 | .WithOne(b => b.Owner);
|
---|
106 |
|
---|
107 | //
|
---|
108 | // Restoraunt
|
---|
109 | //
|
---|
110 | modelBuilder.Entity<Restaurant>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
111 | modelBuilder.Entity<Restaurant>()
|
---|
112 | .HasOne(p => p.Owner)
|
---|
113 | .WithOne(b => b.Restaurant)
|
---|
114 | .HasForeignKey<Restaurant>(k => k.OwnerFk);
|
---|
115 | modelBuilder.Entity<Restaurant>()
|
---|
116 | .HasMany(p => p.Reservations)
|
---|
117 | .WithOne(b => b.Restaurant);
|
---|
118 | modelBuilder.Entity<Restaurant>()
|
---|
119 | .HasMany(p => p.Reviews)
|
---|
120 | .WithOne(b => b.Restaurant);
|
---|
121 |
|
---|
122 | //
|
---|
123 | // Reservation
|
---|
124 | //
|
---|
125 | modelBuilder.Entity<Reservation>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
126 | modelBuilder.Entity<Reservation>()
|
---|
127 | .HasOne(p => p.Restaurant)
|
---|
128 | .WithMany(b => b.Reservations);
|
---|
129 |
|
---|
130 | //
|
---|
131 | // MenuItem
|
---|
132 | //
|
---|
133 | modelBuilder.Entity<MenuItem>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
134 | modelBuilder.Entity<MenuItem>()
|
---|
135 | .HasOne(p => p.Restaurant)
|
---|
136 | .WithMany(b => b.Menu);
|
---|
137 |
|
---|
138 | //
|
---|
139 | // Review
|
---|
140 | //
|
---|
141 | modelBuilder.Entity<Review>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
142 | modelBuilder.Entity<Review>()
|
---|
143 | .HasOne(p => p.Restaurant)
|
---|
144 | .WithMany(b => b.Reviews);
|
---|
145 | modelBuilder.Entity<Review>()
|
---|
146 | .HasOne(p => p.User);
|
---|
147 |
|
---|
148 | //
|
---|
149 | // ToDoItem
|
---|
150 | //
|
---|
151 | modelBuilder.Entity<ToDoItem>().Property(x => x.Id).IsRequired().ValueGeneratedOnAdd();
|
---|
152 | modelBuilder.Entity<ToDoItem>()
|
---|
153 | .HasOne(p => p.LinkedReview);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|