-
Notifications
You must be signed in to change notification settings - Fork 0
feat(product): T2 — EF Core persistence + ORM config (verbatim from monolith) #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devin/product-t1-domain
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Product.Domain.Entities; | ||
|
|
||
| namespace Product.Infrastructure.Data; | ||
|
|
||
|
|
@@ -8,9 +9,26 @@ public ProductDbContext(DbContextOptions<ProductDbContext> options) : base(optio | |
| { | ||
| } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| public DbSet<Product.Domain.Entities.Product> Products => Set<Product.Domain.Entities.Product>(); | ||
| public DbSet<ProductCategory> ProductCategories => Set<ProductCategory>(); | ||
|
|
||
| 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<ProductCategory>().Property(p => p.Name).IsRequired().HasMaxLength(100); | ||
| builder.Entity<ProductCategory>().Property(p => p.Description).HasMaxLength(500); | ||
| builder.Entity<ProductCategory>().ToTable("AppProductCategories"); | ||
|
|
||
| // Product — verbatim from monolith | ||
| builder.Entity<Product.Domain.Entities.Product>().Property(p => p.Name).IsRequired().HasMaxLength(100); | ||
| builder.Entity<Product.Domain.Entities.Product>().HasIndex(p => p.Name); | ||
| builder.Entity<Product.Domain.Entities.Product>().Property(p => p.Description).HasMaxLength(500); | ||
| builder.Entity<Product.Domain.Entities.Product>().Property(p => p.Icon).IsUnicode(false).HasMaxLength(256); | ||
| builder.Entity<Product.Domain.Entities.Product>().HasOne(p => p.Parent).WithMany(p => p.Children).OnDelete(DeleteBehavior.Restrict); | ||
| builder.Entity<Product.Domain.Entities.Product>().Property(p => p.BuyingPrice).HasColumnType("decimal(18,2)"); | ||
| builder.Entity<Product.Domain.Entities.Product>().Property(p => p.SellingPrice).HasColumnType("decimal(18,2)"); | ||
| builder.Entity<Product.Domain.Entities.Product>().ToTable("AppProducts"); | ||
|
Comment on lines
+24
to
+32
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚩 Product-to-ProductCategory relationship relies on EF Core convention rather than explicit configuration The Was this helpful? React with 👍 or 👎 to provide feedback.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct — the Product→ProductCategory relationship uses EF Core's convention (cascade delete for non-nullable FK). This matches the monolith's configuration, which also did not explicitly configure this relationship's delete behavior — only the self-referencing |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚩 EnsureCreated() runs unconditionally in all environments and is incompatible with future migrations
The
db.Database.EnsureCreated()call atsrc/Services/Product/Product.API/Program.cs:19runs in every environment (including production), not just development. This is consistent with the Notification service (src/Services/Notification/Notification.API/Program.cs:26), so it follows the existing codebase pattern. However,EnsureCreated()does not create the__EFMigrationsHistorytable and is fundamentally incompatible with EF Core migrations. If migrations are added later,Database.Migrate()will attempt to apply all migrations from scratch and fail because the tables already exist. This is fine as long as the team is aware they cannot mix the two approaches. If migrations are planned, this should be changed toDatabase.Migrate()before any migration is created.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acknowledged — this is intentionally consistent with the Notification service pattern (
src/Services/Notification/Notification.API/Program.cs:26). The task spec explicitly calls forEnsureCreated()on startup. If the team later adopts EF migrations, this can be swapped toDatabase.Migrate()at that time.