Skip to main content
Jagdish Kumawat
Founder @ Dewiride
View all authors

How to Revert an Applied EF Core Migration

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A migration is applied, and you need it undone: it was a mistake, or you want to test it again from the start. EF Core reverts migrations with the same command that applies them, dotnet ef database update, pointed at an earlier migration. This guide reverts one migration, then all of them, and shows the two things to check before you do it on a database you care about: whether Down can undo the data, and what it deletes.

How to Remove the Last EF Core Migration

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

You ran dotnet ef migrations add, looked at the result, and it's wrong: a missing setting, a typo in the name, a change you didn't mean to include. dotnet ef migrations remove undoes it cleanly, as long as the migration hasn't been applied. This guide removes an unapplied migration, shows the error when it was applied, and the two ways forward from there.

How to Check for Pending Model Changes in EF Core

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Change an entity, forget dotnet ef migrations add, and the code and the database quietly disagree until something fails. EF Core has a command that catches exactly that: dotnet ef migrations has-pending-model-changes. This guide shows both of its answers, what it actually compares, how to use its exit code in a build, and how it proves that a refactor of your model configuration changed nothing.

How to List EF Core Migrations and See Which Ones Are Applied

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Before applying, reverting or scripting migrations, you need two answers: which migrations exist in the code, and which ones this database already has. dotnet ef migrations list gives both in one list. This guide reads its output, including the (Pending) marker, gets the same list as JSON, lists without a database, and looks at the __EFMigrationsHistory table where EF Core keeps the record.

How to Generate a SQL Script from EF Core Migrations

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

dotnet ef database update applies migrations straight to a database. Often you want the SQL instead: to read exactly what will run, to have a database administrator review it, or to apply it where the tools can't reach. dotnet ef migrations script generates that SQL from your migrations. This guide covers every form of the command with real scripts for MySQL, and shows why reading the SQL catches problems the C# migration hides.

How to Use EF Core Migrations When the DbContext Is in a Class Library

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Keeping the DbContext and its entities in a class library, separate from the web API, is a clean way to structure a .NET C# solution. It also means dotnet ef needs to know about two projects, not one, and the tools' errors when it doesn't are some of the most searched in EF Core. This guide explains the two roles, shows all three errors live, and gives the command that works.

How to Use ConfigureConventions in EF Core to Configure Every Property of a Type

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Some rules apply to every property of a type: all money is decimal(12,2), every DateTime is UTC, every enum is stored by name. Writing those rules property by property is repetitive, and the next property someone adds will miss them. EF Core's ConfigureConventions sets each rule once for the whole model. This guide writes three such rules, proves each one leaves the model unchanged when it replaces per-property code, and shows how a single property can still be different.

How to Configure a One-to-Many Relationship in EF Core

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

One category has many products; each product belongs to one category. That's a one-to-many relationship, the most common shape in any database. EF Core can discover it from your classes alone, and it's worth knowing exactly what it decides for you before you configure anything. This guide models it in C#, shows the MySQL foreign key, index and delete rule EF Core creates by convention, and then configures the same relationship explicitly.

How to Make EF Core Columns Required or Nullable with C# Nullable Reference Types

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

Whether a database column may be empty should be decided once, in your C# code, and enforced everywhere: by the compiler, by EF Core and by MySQL. With nullable reference types turned on, EF Core does exactly that: string? becomes a column that allows NULL, string one that doesn't. This guide shows the rule in a real migration, adds C#'s required modifier so the compiler catches missing values, and changes an existing column from nullable to required without breaking.

How to Set a Maximum String Length in EF Core (varchar Instead of longtext in MySQL)

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A C# string has no length limit, so without being told otherwise, EF Core's MySQL provider gives each one the biggest text type MySQL has: longtext, up to 4 GB. That's wasteful for a SKU, and it gets in the way of indexes. This guide sets real maximum lengths so columns become varchar(n), applies the change to a table that already has data, and shows what happens when you index a string without a length.