T2(customer): add EF Core persistence with ORM config and initial migration#62
Conversation
…ration - Configure CustomerDbContext with ORM mappings from monolith (AppCustomers table) - Add DbSet<Customer> property and entity configuration (indexes, max lengths) - Generate InitialCreate EF Core migration - Add EnsureCreated on startup in Program.cs - Verified migration applies against PostgreSQL 16
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
| using (var scope = app.Services.CreateScope()) | ||
| { | ||
| var db = scope.ServiceProvider.GetRequiredService<CustomerDbContext>(); | ||
| db.Database.EnsureCreated(); |
There was a problem hiding this comment.
🔴 Database setup method conflicts with migrations, making future schema updates fail
The database is created using a shortcut method (EnsureCreated() at src/Services/Customer/Customer.API/Program.cs:19) instead of applying the migrations that the PR also introduces, so the migration history table is never populated and any future migration will crash because the tables already exist.
Impact: After the first startup, all subsequent schema migrations will fail with "table already exists" errors, making the migration files in this PR useless.
EnsureCreated vs Migrate conflict mechanism
db.Database.EnsureCreated() creates the schema directly without recording anything in the __EFMigrationsHistory table. The PR also adds a full EF Core migration (src/Services/Customer/Customer.Infrastructure/Data/Migrations/20260626044302_InitialCreate.cs) which expects to be tracked in that history table.
When a developer later adds a second migration and runs db.Database.Migrate() or dotnet ef database update, EF Core will see no migration history and attempt to apply InitialCreate, which will fail because the AppCustomers table already exists.
The RECON.md at src/Services/Customer/RECON.md:115 states the T2 validation gate requires: "EF migration applies against PostgreSQL" — this cannot work with EnsureCreated().
The fix is to replace db.Database.EnsureCreated() with db.Database.Migrate() so the migration is applied and tracked properly.
| db.Database.EnsureCreated(); | |
| db.Database.Migrate(); |
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Valid point about EnsureCreated() vs Migrate() for long-term migration tracking. However, this follows the existing pattern used by the Notification service (Notification.API/Program.cs:26) and was explicitly requested in the task spec. Deferring to the task author on whether to switch to Migrate() in a follow-up.
Summary
Configures
CustomerDbContextwith ORM mappings ported verbatim from the monolith, generates the initial EF Core migration, and wires upEnsureCreatedon startup.DbContext changes (
CustomerDbContext.cs):Startup (
Program.cs): addsdb.Database.EnsureCreated()in a scoped block afterbuilder.Build(), matching the Notification service pattern.Migration:
InitialCreatecreatesAppCustomerstable with PK, all columns with correct types/constraints, andIX_AppCustomers_Nameindex.Verified against PostgreSQL 16 — migration applies cleanly and produces the expected schema.
Part of the Customer microservice carve-out stacked PR chain (T2/T5).
Link to Devin session: https://partner-workshops.devinenterprise.com/sessions/df4fd98794c34738bf5a70c588276dcb
Requested by: @mbatchelor81