Skip to content

T2(customer): add EF Core persistence with ORM config and initial migration#62

Open
devin-ai-integration[bot] wants to merge 1 commit into
devin/customer-service-t1from
devin/customer-service-t2
Open

T2(customer): add EF Core persistence with ORM config and initial migration#62
devin-ai-integration[bot] wants to merge 1 commit into
devin/customer-service-t1from
devin/customer-service-t2

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Configures CustomerDbContext with ORM mappings ported verbatim from the monolith, generates the initial EF Core migration, and wires up EnsureCreated on startup.

DbContext changes (CustomerDbContext.cs):

using Entities = Customer.Domain.Entities; // alias to disambiguate from root `Customer` namespace

public DbSet<Entities.Customer> Customers => Set<Entities.Customer>();

modelBuilder.Entity<Entities.Customer>(entity => {
    entity.ToTable("AppCustomers");
    entity.Property(c => c.Name).IsRequired().HasMaxLength(100);
    entity.HasIndex(c => c.Name);
    entity.Property(c => c.Email).HasMaxLength(100);
    entity.Property(c => c.PhoneNumber).IsUnicode(false).HasMaxLength(30);
    entity.Property(c => c.City).HasMaxLength(50);
    entity.Property(c => c.CreatedBy).HasMaxLength(40);
    entity.Property(c => c.UpdatedBy).HasMaxLength(40);
});

Startup (Program.cs): adds db.Database.EnsureCreated() in a scoped block after builder.Build(), matching the Notification service pattern.

Migration: InitialCreate creates AppCustomers table with PK, all columns with correct types/constraints, and IX_AppCustomers_Name index.

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


Open in Devin Review

…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
@mbatchelor81 mbatchelor81 self-assigned this Jun 26, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Open in Devin Review

using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<CustomerDbContext>();
db.Database.EnsureCreated();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 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.

Suggested change
db.Database.EnsureCreated();
db.Database.Migrate();
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant