/
ApplicationDbContext.cs
56 lines (46 loc) · 1.98 KB
/
ApplicationDbContext.cs
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
56
using FruitAndVegetableWarehouseManagement.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace FruitAndVegetableWarehouseManagement.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Product> Products { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Invoice> Invoices { get; set; }
public DbSet<Supply> Supplies { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<InvoiceProduct>()
.HasKey(ip => new { ip.InvoiceID, ip.ProductID });
modelBuilder.Entity<InvoiceProduct>()
.HasOne(ip => ip.Invoice)
.WithMany(i => i.InvoiceProducts)
.HasForeignKey(ip => ip.InvoiceID);
modelBuilder.Entity<InvoiceProduct>()
.HasOne(ip => ip.Product)
.WithMany(p => p.InvoiceProducts)
.HasForeignKey(ip => ip.ProductID);
modelBuilder.Entity<SupplyProduct>()
.HasKey(sp => new {sp.SupplyId, sp.ProductId});
modelBuilder.Entity<SupplyProduct>()
.HasOne(sp => sp.Supply)
.WithMany(s => s.SupplyProducts)
.HasForeignKey(sp => sp.SupplyId);
modelBuilder.Entity<SupplyProduct>()
.HasOne(sp => sp.Product)
.WithMany(p => p.SupplyProducts)
.HasForeignKey(sp => sp.ProductId);
modelBuilder.Entity<Company>()
.HasDiscriminator<string>("CompanyType")
.HasValue<Customer>("customer")
.HasValue<Supplier>("supplier");
base.OnModelCreating(modelBuilder);
}
}
}