-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplicationDbContext.cs
More file actions
55 lines (44 loc) · 1.84 KB
/
Copy pathApplicationDbContext.cs
File metadata and controls
55 lines (44 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using E_CommerceSystem.Models;
using Microsoft.EntityFrameworkCore;
namespace E_CommerceSystem
{
public class ApplicationDbContext:DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<OrderProducts> OrderProducts { get; set; }
public DbSet<Review> Reviews { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<ProductImage> ProductImages { get; set; } // DbSet for ProductImage entity
public DbSet<Supplier> Suppliers { get; set; }
public DbSet<RefreshToken> RefreshTokens { get; set; }//add RefreshTokens DbSet to ApplicationDbContext
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();
// Category
modelBuilder.Entity<Category>()
.HasIndex(c => c.Name).IsUnique();
// Product-Category (Many-to-One)
modelBuilder.Entity<Product>()
.HasOne(p => p.Categoty)
.WithMany(c => c.Products)
.HasForeignKey(p => p.CategoryId);
modelBuilder.Entity<Order>()
.Property(o => o.Status)
.HasConversion<string>();
modelBuilder.Entity<ProductImage>()
.HasOne(pi => pi.Product)
.WithMany(p => p.ProductImages)
.HasForeignKey(pi => pi.PID);
modelBuilder.Entity<Review>()
.HasIndex(r => new { r.PID, r.UID })
.IsUnique();
}
}
}