DataContext.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Microsoft.EntityFrameworkCore;
  2. using Products.Business.Domain;
  3. using Products.Common.Types;
  4. namespace Products.Business.Persistence;
  5. public sealed class DataContext : DbContext
  6. {
  7. public required DbSet<Product> Products { get; set; }
  8. public required DbSet<User> Users { get; set; }
  9. public DataContext() { }
  10. public DataContext(DbContextOptions<DataContext> options) : base(options) { }
  11. protected override void OnConfiguring
  12. (DbContextOptionsBuilder optionsBuilder)
  13. {
  14. optionsBuilder.UseSqlite("Data Source=../products.db");
  15. }
  16. protected override void OnModelCreating(ModelBuilder modelBuilder)
  17. {
  18. // seed the database with dummy data
  19. modelBuilder.Entity<Product>().HasData(
  20. new Product
  21. {
  22. Id = 1,
  23. Name = "Product Azul",
  24. Brand = "Brand Azul",
  25. Color = Color.Blue
  26. },
  27. new Product
  28. {
  29. Id = 2,
  30. Name = "Product Rojo",
  31. Brand = "Brand Rojo",
  32. Color = Color.Red
  33. },
  34. new Product
  35. {
  36. Id = 3,
  37. Name = "Product Verde",
  38. Brand = "Brand Verde",
  39. Color = Color.Green
  40. },
  41. new Product
  42. {
  43. Id = 4,
  44. Name = "Product Azul #2",
  45. Brand = "Brand Azul #2",
  46. Color = Color.Blue
  47. },
  48. new Product
  49. {
  50. Id = 5,
  51. Name = "Product Rojo #2",
  52. Brand = "Brand Rojo #2",
  53. Color = Color.Red
  54. }
  55. );
  56. base.OnModelCreating(modelBuilder);
  57. }
  58. }