Friday, April 25, 2025

Sample Database Migration Plan for EF-Core to FluentMigrator

 Executive Summary

A monolithic web solution (“MainWeb”) and a companion service (“Jobs.Api”) both use Entity Framework Core (EF Core) migrations against a shared PostgreSQL database. Maintaining duplicate entity definitions and migrations across two projects is inefficient—especially because Jobs.Api intends to drop EF Core altogether. This proposal outlines migrating both codebases to FluentMigrator, a framework-agnostic migration tool. A dedicated migration project will become the single source of truth, while Jobs.Api moves to Dapper for data access and MainWeb keeps EF Core strictly for querying. The approach streamlines schema management and supports an eventual move away from a monolith.


Current Situation

ComponentStatus
MainWeb (Monolith)Uses EF Core for data access and migrations; migration history lives in __EFMigrationsHistory.
Jobs.ApiShares database objects with MainWeb; currently EF Core-based but aiming to remove that dependency.
Shared DatabaseOne PostgreSQL instance managed by MainWeb’s EF Core migrations, requiring tight coordination.

Challenges

  • Duplication — Entities exist in two codebases, creating redundant work.

  • Migration Coordination — Two different data-access strategies must agree on each schema change.

  • EF Core Dependency — Jobs.Api’s desire to drop EF Core is blocked by MainWeb’s migration workflow.

  • Monolith Constraints — MainWeb’s size makes a full rewrite impractical in the short term.


Proposed Solution

PillarDetails
Central Migration ProjectCreate Database.Migrations, powered by FluentMigrator. It owns every schema change.
Jobs.Api TransitionRemove EF Core, adopt Dapper, and apply migrations via the shared FluentMigrator project.
MainWeb AdaptationKeep EF Core for querying, but stop using EF Core migrations. After each FluentMigrator run, manually synchronize the EF Core model.
Long-Term PathGradually refactor MainWeb into smaller services that also use FluentMigrator and lightweight data-access libraries.

Implementation Plan

StepObjectiveKey Actions
1. Create Database.MigrationsCentralize schema changes- Add FluentMigrator & Runner.
- Configure Postgres provider and VersionInfo table.
- Add an InitialMigration that marks today’s schema as baseline.
2. Transition Jobs.ApiEliminate EF Core- Remove EF Core packages.
- Add Dapper.
- Apply migrations on startup via the Runner.
3. Adapt MainWebKeep EF Core for data access only- Disable context.Database.Migrate() calls.
- Reference Database.Migrations.
- Update entities/DbContext after each migration.
4. Coordinate Schema ChangesPrevent drift- Author new migrations in Database.Migrations.
- CI/CD applies them.
- Jobs.Api auto-updates; MainWeb updates its model manually.
5. Preserve Existing EF Core HistoryAvoid disruption- Keep __EFMigrationsHistory as-is.
- Treat the initial FluentMigrator baseline as authoritative going forward.

Benefits

  • Single Source of Truth — One migration pipeline eliminates drift.

  • Flexibility for Jobs.Api — Dapper + FluentMigrator removes heavyweight ORM overhead.

  • Future-Proofing — Aligns with a gradual break-up of the monolith.

  • Version Control Friendly — FluentMigrator’s C# migrations are easy to review in Git.


Risks & Mitigations

RiskMitigation
Manual EF Core model updates may lagDocument the process; add validation tests to catch mismatches.
Extra coordination overheadDefine a clear “write-migration-first” workflow and automate via CI/CD.
Learning curve for FluentMigratorProvide quick-start guides and internal training sessions.
Long-term MainWeb refactor effortPlan phased service extraction to spread effort over time.

Timeline (Indicative)

PhaseTasksDuration
Setup Migration ProjectCreate Database.Migrations, configure baseline1–2 weeks
Transition Jobs.ApiRemove EF Core, add Dapper, wire up migrations2–3 weeks
Adapt MainWebDisable EF Core migrations, document model-sync steps1–2 weeks
Process & CI/CDFormalize workflow, automate migration execution1 week
Long-Term RefactorGradual decomposition of the monolithOngoing

Conclusion

Moving to FluentMigrator centralizes database evolution, lets Jobs.Api shed EF Core, and positions the system for a future microservice architecture. By balancing short-term practicality with long-term flexibility, this plan reduces duplication, lowers maintenance overhead, and creates a clear, auditable history of schema changes.

Checkout my FluentRun project on Github. It is a FluentMigrator tool that runs seamlessly with any .net project: https://github.com/DotNetDeveloperDan/FluentRun

No comments:

Post a Comment

New Features in .Net 10

🚀 Runtime Enhancements Stack Allocation for Small Arrays The Just-In-Time (JIT) compiler now optimizes memory usage by stack-allocating s...