diff --git a/src/Services/Product/Product.API/Program.cs b/src/Services/Product/Product.API/Program.cs index 73146ef..0ae7c5f 100644 --- a/src/Services/Product/Product.API/Program.cs +++ b/src/Services/Product/Product.API/Program.cs @@ -13,6 +13,12 @@ var app = builder.Build(); +using (var scope = app.Services.CreateScope()) +{ + var db = scope.ServiceProvider.GetRequiredService(); + db.Database.EnsureCreated(); +} + if (app.Environment.IsDevelopment()) { app.UseSwagger(); diff --git a/src/Services/Product/Product.Infrastructure/Data/ProductDbContext.cs b/src/Services/Product/Product.Infrastructure/Data/ProductDbContext.cs index 37cfb81..c38c688 100644 --- a/src/Services/Product/Product.Infrastructure/Data/ProductDbContext.cs +++ b/src/Services/Product/Product.Infrastructure/Data/ProductDbContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using Product.Domain.Entities; namespace Product.Infrastructure.Data; @@ -8,9 +9,26 @@ public ProductDbContext(DbContextOptions options) : base(optio { } - protected override void OnModelCreating(ModelBuilder modelBuilder) + public DbSet Products => Set(); + public DbSet ProductCategories => Set(); + + protected override void OnModelCreating(ModelBuilder builder) { - base.OnModelCreating(modelBuilder); - // TODO: Configure entity mappings migrated from monolith + base.OnModelCreating(builder); + + // ProductCategory — verbatim from monolith + builder.Entity().Property(p => p.Name).IsRequired().HasMaxLength(100); + builder.Entity().Property(p => p.Description).HasMaxLength(500); + builder.Entity().ToTable("AppProductCategories"); + + // Product — verbatim from monolith + builder.Entity().Property(p => p.Name).IsRequired().HasMaxLength(100); + builder.Entity().HasIndex(p => p.Name); + builder.Entity().Property(p => p.Description).HasMaxLength(500); + builder.Entity().Property(p => p.Icon).IsUnicode(false).HasMaxLength(256); + builder.Entity().HasOne(p => p.Parent).WithMany(p => p.Children).OnDelete(DeleteBehavior.Restrict); + builder.Entity().Property(p => p.BuyingPrice).HasColumnType("decimal(18,2)"); + builder.Entity().Property(p => p.SellingPrice).HasColumnType("decimal(18,2)"); + builder.Entity().ToTable("AppProducts"); } }