|
| 1 | +using Lesson07.Models; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using System.Windows.Controls; |
| 4 | + |
| 5 | +namespace Lesson07.Data |
| 6 | +{ |
| 7 | + public class InventoryDbContext : DbContext |
| 8 | + { |
| 9 | + public virtual DbSet<Category> Categories { get; set; } |
| 10 | + public virtual DbSet<Product> Products { get; set; } |
| 11 | + public virtual DbSet<Supplier> Suppliers { get; set; } |
| 12 | + public virtual DbSet<Supply> Supplies { get; set; } |
| 13 | + public virtual DbSet<SupplyProduct> SupplyProducts { get; set; } |
| 14 | + public virtual DbSet<Customer> Customers { get; set; } |
| 15 | + public virtual DbSet<Sale> Sales { get; set; } |
| 16 | + public virtual DbSet<SaleProduct> SalesProducts { get; set; } |
| 17 | + |
| 18 | + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) |
| 19 | + { |
| 20 | + optionsBuilder.UseSqlServer("Data Source=WIN-37KMC9AKIMF;Initial Catalog=InventoryManagement;Integrated Security=True;Encrypt=False"); |
| 21 | + |
| 22 | + base.OnConfiguring(optionsBuilder); |
| 23 | + } |
| 24 | + |
| 25 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 26 | + { |
| 27 | + #region Category |
| 28 | + |
| 29 | + modelBuilder.Entity<Category>() |
| 30 | + .ToTable(nameof(Category)); |
| 31 | + modelBuilder.Entity<Category>() |
| 32 | + .HasKey(x => x.Id); |
| 33 | + modelBuilder.Entity<Category>() |
| 34 | + .Property(x => x.Name) |
| 35 | + .HasMaxLength(150) |
| 36 | + .IsRequired(); |
| 37 | + modelBuilder.Entity<Category>() |
| 38 | + .HasMany(c => c.Products) |
| 39 | + .WithOne(p => p.Category) |
| 40 | + .HasForeignKey(p => p.CategoryId); |
| 41 | + |
| 42 | + #endregion |
| 43 | + |
| 44 | + #region Product |
| 45 | + |
| 46 | + modelBuilder.Entity<Product>() |
| 47 | + .ToTable(nameof(Product)); |
| 48 | + modelBuilder.Entity<Product>() |
| 49 | + .HasKey(x => x.Id); |
| 50 | + modelBuilder.Entity<Product>() |
| 51 | + .Property(x => x.Name) |
| 52 | + .HasMaxLength(250) |
| 53 | + .IsRequired(); |
| 54 | + modelBuilder.Entity<Product>() |
| 55 | + .Property(x => x.Description) |
| 56 | + .HasMaxLength(500) |
| 57 | + .IsRequired(false); |
| 58 | + modelBuilder.Entity<Product>() |
| 59 | + .Property(x => x.Price) |
| 60 | + .HasColumnType("money") |
| 61 | + .HasDefaultValue(0) |
| 62 | + .HasPrecision(18, 2); |
| 63 | + modelBuilder.Entity<Product>() |
| 64 | + .Property(x => x.ExpireDate) |
| 65 | + .IsRequired(); |
| 66 | + |
| 67 | + modelBuilder.Entity<Product>() |
| 68 | + .HasOne(p => p.Category) |
| 69 | + .WithMany(c => c.Products) |
| 70 | + .HasForeignKey(p => p.CategoryId); |
| 71 | + modelBuilder.Entity<Product>() |
| 72 | + .HasMany(p => p.SupplyProducts) |
| 73 | + .WithOne(sp => sp.Product) |
| 74 | + .HasForeignKey(sp => sp.ProductId); |
| 75 | + modelBuilder.Entity<Product>() |
| 76 | + .HasMany(sp => sp.SaleProducts) |
| 77 | + .WithOne(p => p.Product); |
| 78 | + |
| 79 | + #endregion |
| 80 | + |
| 81 | + #region Supplier |
| 82 | + |
| 83 | + modelBuilder.Entity<Supplier>() |
| 84 | + .ToTable(nameof(Supplier)); |
| 85 | + modelBuilder.Entity<Supplier>() |
| 86 | + .HasKey(x => x.Id); |
| 87 | + modelBuilder.Entity<Supplier>() |
| 88 | + .Property(x => x.FirstName) |
| 89 | + .HasMaxLength(150) |
| 90 | + .IsRequired(); |
| 91 | + modelBuilder.Entity<Supplier>() |
| 92 | + .Property(x => x.LastName) |
| 93 | + .HasMaxLength(150) |
| 94 | + .IsRequired(false); |
| 95 | + modelBuilder.Entity<Supplier>() |
| 96 | + .Property(x => x.Company) |
| 97 | + .HasMaxLength(255) |
| 98 | + .IsRequired(); |
| 99 | + modelBuilder.Entity<Supplier>() |
| 100 | + .Property(x => x.PhoneNumber) |
| 101 | + .HasMaxLength(13) |
| 102 | + .IsRequired(); |
| 103 | + |
| 104 | + #endregion |
| 105 | + |
| 106 | + #region Supply |
| 107 | + |
| 108 | + modelBuilder.Entity<Supply>() |
| 109 | + .ToTable(nameof(Supply)); |
| 110 | + modelBuilder.Entity<Supply>() |
| 111 | + .HasKey(x => x.Id); |
| 112 | + modelBuilder.Entity<Supply>() |
| 113 | + .Property(x => x.Date) |
| 114 | + .IsRequired(); |
| 115 | + modelBuilder.Entity<Supply>() |
| 116 | + .Property(x => x.TotalDue) |
| 117 | + .HasColumnType("money") |
| 118 | + .HasPrecision(18, 2); |
| 119 | + modelBuilder.Entity<Supply>() |
| 120 | + .Property(x => x.TotalPaid) |
| 121 | + .HasColumnType("money") |
| 122 | + .HasPrecision(18, 2); |
| 123 | + |
| 124 | + modelBuilder.Entity<Supply>() |
| 125 | + .HasMany(x => x.SupplyProducts) |
| 126 | + .WithOne(sp => sp.Supply) |
| 127 | + .HasForeignKey(sp => sp.SupplyId); |
| 128 | + |
| 129 | + #endregion |
| 130 | + |
| 131 | + #region Supply Product |
| 132 | + |
| 133 | + modelBuilder.Entity<SupplyProduct>() |
| 134 | + .ToTable(nameof(SupplyProduct)); |
| 135 | + modelBuilder.Entity<SupplyProduct>() |
| 136 | + .HasKey(x => x.Id); |
| 137 | + modelBuilder.Entity<SupplyProduct>() |
| 138 | + .Property(x => x.Quantity) |
| 139 | + .IsRequired() |
| 140 | + .HasDefaultValue(1); |
| 141 | + modelBuilder.Entity<SupplyProduct>() |
| 142 | + .Property(x => x.UnitPrice) |
| 143 | + .HasColumnType("money") |
| 144 | + .HasPrecision(18, 2) |
| 145 | + .IsRequired(); |
| 146 | + |
| 147 | + modelBuilder.Entity<SupplyProduct>() |
| 148 | + .HasOne(sp => sp.Supply) |
| 149 | + .WithMany(s => s.SupplyProducts) |
| 150 | + .HasForeignKey(sp => sp.SupplyId); |
| 151 | + modelBuilder.Entity<SupplyProduct>() |
| 152 | + .HasOne(sp => sp.Product) |
| 153 | + .WithMany(p => p.SupplyProducts) |
| 154 | + .HasForeignKey(sp => sp.ProductId); |
| 155 | + |
| 156 | + #endregion |
| 157 | + |
| 158 | + #region Customer |
| 159 | + |
| 160 | + modelBuilder.Entity<Customer>() |
| 161 | + .ToTable(nameof(Customer)); |
| 162 | + modelBuilder.Entity<Customer>() |
| 163 | + .HasKey(x => x.Id); |
| 164 | + modelBuilder.Entity<Customer>() |
| 165 | + .Property(x => x.FirstName) |
| 166 | + .HasMaxLength(50) |
| 167 | + .IsRequired(); |
| 168 | + modelBuilder.Entity<Customer>() |
| 169 | + .Property(x => x.LastName) |
| 170 | + .HasMaxLength(150) |
| 171 | + .IsRequired(); |
| 172 | + modelBuilder.Entity<Customer>() |
| 173 | + .Property(x => x.PhoneNumber) |
| 174 | + .HasMaxLength(13) |
| 175 | + .IsRequired(); |
| 176 | + modelBuilder.Entity<Customer>() |
| 177 | + .Property(x => x.Address) |
| 178 | + .HasMaxLength(100) |
| 179 | + .IsRequired(false); |
| 180 | + |
| 181 | + modelBuilder.Entity<Customer>() |
| 182 | + .HasMany(c => c.Sales) |
| 183 | + .WithOne(s => s.Customer) |
| 184 | + .HasForeignKey(s => s.CustomerId); |
| 185 | + |
| 186 | + |
| 187 | + #endregion |
| 188 | + |
| 189 | + #region Sale |
| 190 | + |
| 191 | + modelBuilder.Entity <Sale>() |
| 192 | + .ToTable(nameof(Sale)); |
| 193 | + modelBuilder.Entity<Sale>() |
| 194 | + .HasKey(x => x.Id); |
| 195 | + modelBuilder.Entity<Sale>() |
| 196 | + .Property(x => x.SaleDate) |
| 197 | + .IsRequired(); |
| 198 | + modelBuilder.Entity<Sale>() |
| 199 | + .Property(x => x.TotalDue) |
| 200 | + .HasColumnType("money") |
| 201 | + .HasPrecision(18, 2) |
| 202 | + .IsRequired(); |
| 203 | + modelBuilder.Entity<Sale>() |
| 204 | + .Property(x => x.TotalPaid) |
| 205 | + .HasColumnType("money") |
| 206 | + .HasPrecision(18, 2) |
| 207 | + .IsRequired(); |
| 208 | + modelBuilder.Entity<Sale>() |
| 209 | + .Property(x => x.TotalDiscount) |
| 210 | + .HasPrecision(18, 2) |
| 211 | + .HasDefaultValue(0) |
| 212 | + .IsRequired(); |
| 213 | + |
| 214 | + modelBuilder.Entity<Sale>() |
| 215 | + .HasOne(s => s.Customer) |
| 216 | + .WithMany(c => c.Sales) |
| 217 | + .HasForeignKey(s => s.CustomerId); |
| 218 | + modelBuilder.Entity<Sale>() |
| 219 | + .HasMany(s => s.SaleProducts) |
| 220 | + .WithOne(sp => sp.Sale) |
| 221 | + .HasForeignKey(sp => sp.SaleId); |
| 222 | + |
| 223 | + #endregion |
| 224 | + |
| 225 | + #region SaleProduct |
| 226 | + |
| 227 | + modelBuilder.Entity<SaleProduct>() |
| 228 | + .ToTable(nameof(SaleProduct)); |
| 229 | + modelBuilder.Entity<SaleProduct>() |
| 230 | + .HasKey(x => x.Id); |
| 231 | + |
| 232 | + modelBuilder.Entity<SaleProduct>() |
| 233 | + .Property(x => x.Quantity) |
| 234 | + .HasDefaultValue(1); |
| 235 | + modelBuilder.Entity<SaleProduct>() |
| 236 | + .Property(x => x.UnitPrice) |
| 237 | + .HasColumnType("money") |
| 238 | + .HasPrecision(18, 2) |
| 239 | + .IsRequired(); |
| 240 | + modelBuilder.Entity<SaleProduct>() |
| 241 | + .Property(x => x.Discount) |
| 242 | + .HasPrecision(18, 2) |
| 243 | + .HasColumnType("money") |
| 244 | + .IsRequired(); |
| 245 | + |
| 246 | + modelBuilder.Entity<SaleProduct>() |
| 247 | + .HasOne(sp => sp.Sale) |
| 248 | + .WithMany(s => s.SaleProducts) |
| 249 | + .HasForeignKey(sp => sp.SaleId); |
| 250 | + modelBuilder.Entity<SaleProduct>() |
| 251 | + .HasOne(sp => sp.Product) |
| 252 | + .WithMany(p => p.SaleProducts) |
| 253 | + .HasForeignKey(sp => sp.ProductId); |
| 254 | + |
| 255 | + #endregion |
| 256 | + |
| 257 | + base.OnModelCreating(modelBuilder); |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments