Prilixor
All blogs

Databases

Database Migrations with Entity Framework Core: Best Practices and Pitfalls

Managing database schema changes can be one of the trickiest aspects of application development. With Entity Framework (EF) Core, developers gain a powerful tool for handling migrations — but it also introduces potential pitfalls if not managed properly. Let’s explore how to use EF Core Migrations effectively, avoid common issues, and maintain production stability.

· 3 min read
Share

Managing database schema changes can be one of the trickiest aspects of application development. With Entity Framework (EF) Core, developers gain a powerful tool for handling migrations — but it also introduces potential pitfalls if not managed properly. Let’s explore how to use EF Core Migrations effectively, avoid common issues, and maintain production stability.

Understanding EF Core Migrations

Entity Framework Core Migrations is a mechanism that tracks changes to your data model and applies those changes to the database schema automatically. Instead of manually writing SQL scripts, you use EF Core to generate migration files that define the schema evolution.

Example workflow:

  1. Modify your data model (e.g., add a property or entity).
  2. Run Add-Migration <Name> to generate the migration file.
  3. Run Update-Database to apply changes.

This helps developers maintain a version-controlled, incremental record of schema changes across environments.

Best Practices for EF Core Migrations

1. Use Migrations as Part of Version Control

Always commit your migration files alongside your code. This ensures that every schema change corresponds to a specific code change — making it easier to track, roll back, or debug database updates.

2. Apply Migrations in a Controlled Environment

Avoid applying migrations directly from development environments to production. Instead:

  • Use deployment pipelines (CI/CD) to apply migrations in a predictable manner.
  • Test each migration in staging before going live.
  • Use the –verbose flag to review generated SQL for any unexpected schema modifications.

3. Handle Merge Conflicts Carefully

In team environments, multiple developers might create migrations at the same time. This can lead to conflicts. Tip:

  • Rebase and merge migrations in order, ensuring each migration’s timestamp and sequence remain consistent.
  • Use dotnet ef migrations script to generate a cumulative SQL script that merges all changes cleanly.

4. Use Custom Migrations for Complex Scenarios

Sometimes EF Core can’t generate the desired schema automatically (e.g., renaming a column without data loss or restructuring relationships). Solution: Edit the migration file manually — adding custom SQL operations using migrationBuilder.Sql() for precise control.

5. Avoid Auto-Migrations in Production

While EF Core supports automatic migrations, it’s risky for production environments. Auto-migrations can cause data loss or schema mismatches if applied without review. Instead, always:

  • Generate migrations manually.
  • Review the SQL output.
  • Apply through controlled deployments.

Common Pitfalls to Avoid

🚫 Ignoring failed migrations: If a migration fails during deployment, fix and reapply instead of creating a new one over it — this maintains version consistency.

🚫 Deleting migration files after applying: Even after applying a migration, keep its file in version control for traceability and rollback.

🚫 Relying on EF Core for all schema changes: For large datasets or complex restructuring, consider using SQL scripts for better control and safety.

Production-Ready Migration Strategy

A robust production approach includes:

  • Migration scripts generated and reviewed before deployment.
  • Backups of databases before applying updates.
  • Transactional migrations to ensure rollbacks on failure.
  • Logging of applied migrations to track environment status.

Final Thoughts

EF Core Migrations is an invaluable tool for maintaining database evolution in .NET applications. When used thoughtfully, it simplifies schema management and reduces manual errors. But like any automation, it requires discipline — version control, review, and controlled deployment are the keys to using it safely and effectively.

By following best practices and avoiding common pitfalls, teams can ensure their databases evolve smoothly alongside their applications — without downtime or surprises.

Keep reading

Related posts

All blogs →

Azure & Cloud 3 min read

Avoiding Vendor Lock-In While Still Using Azure Effectively

In today's fast-paced digital landscape, organizations are continually adopting cloud solutions to drive innovation and improve efficiency. However, while platforms like Azure offer powerful tools and services, they also present the challenge of vendor lock-in. This can limit an organization’s flexibility to migrate applications or services as needs evolve. The key to leveraging Azure’s capabilities effectively lies in strategic planning and decision-making.

1 Sep 2026

Security 3 min read

Understanding Security Boundaries in Azure: Where Trust Actually Ends

In an era dominated by rapid digital transformation, understanding security boundaries in cloud environments has never been more crucial. Azure, one of the leading cloud service providers, offers a plethora of tools and services designed to enhance security. However, it's imperative to acknowledge that even the most sophisticated systems have limitations. Trust and security boundaries define where end-to-end protections end and where potential vulnerabilities may arise.

31 Aug 2026

Azure & Cloud 3 min read

Navigating Cold Starts, Scaling, and Throttling in Azure Services

In today's digital landscape, the demand for scalable and efficient cloud solutions has never been greater. Organizations rely on cloud services to remain agile, yet they often face challenges that can hinder performance and user experience. Among these are cold starts, scaling decisions, and throttling constraints, particularly within Azure services.

30 Aug 2026