ApplicationDbContext.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // ======================================
  2. // Author: Ebenezer Monney
  3. // Email: info@ebenmonney.com
  4. // Copyright (c) 2017 www.ebenmonney.com
  5. //
  6. // ==> Gun4Hire: contact@ebenmonney.com
  7. // ======================================
  8. using DAL.Models;
  9. using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
  10. using Microsoft.EntityFrameworkCore;
  11. using Microsoft.EntityFrameworkCore.Metadata;
  12. using OpenIddict;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. namespace DAL
  19. {
  20. public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
  21. {
  22. public DbSet<Customer> Customers { get; set; }
  23. public DbSet<ProductCategory> ProductCategories { get; set; }
  24. public DbSet<Product> Products { get; set; }
  25. public DbSet<Order> Orders { get; set; }
  26. public DbSet<OrderDetail> OrderDetails { get; set; }
  27. public ApplicationDbContext(DbContextOptions options) : base(options)
  28. { }
  29. protected override void OnModelCreating(ModelBuilder builder)
  30. {
  31. base.OnModelCreating(builder);
  32. builder.Entity<Customer>().Property(c => c.Name).IsRequired().HasMaxLength(100);
  33. builder.Entity<Customer>().HasIndex(c => c.Name);
  34. builder.Entity<Customer>().Property(c => c.Email).HasMaxLength(100);
  35. builder.Entity<Customer>().Property(c => c.PhoneNumber).IsUnicode(false).HasMaxLength(30);
  36. builder.Entity<Customer>().Property(c => c.City).HasMaxLength(50);
  37. builder.Entity<Customer>().ToTable($"App{nameof(this.Customers)}");
  38. builder.Entity<ProductCategory>().Property(p => p.Name).IsRequired().HasMaxLength(100);
  39. builder.Entity<ProductCategory>().Property(p => p.Description).HasMaxLength(500);
  40. builder.Entity<ProductCategory>().ToTable($"App{nameof(this.ProductCategories)}");
  41. builder.Entity<Product>().Property(p => p.Name).IsRequired().HasMaxLength(100);
  42. builder.Entity<Product>().HasIndex(p => p.Name);
  43. builder.Entity<Product>().Property(p => p.Description).HasMaxLength(500);
  44. builder.Entity<Product>().Property(p => p.Icon).IsUnicode(false).HasMaxLength(256);
  45. builder.Entity<Product>().HasOne(p => p.Parent).WithMany(p => p.Children).OnDelete(DeleteBehavior.Restrict);
  46. builder.Entity<Product>().ToTable($"App{nameof(this.Products)}");
  47. builder.Entity<Order>().Property(o => o.Comments).HasMaxLength(500);
  48. builder.Entity<Order>().ToTable($"App{nameof(this.Orders)}");
  49. builder.Entity<OrderDetail>().ToTable($"App{nameof(this.OrderDetails)}");
  50. }
  51. }
  52. }