Changing the Schema Without Losing Data
EF Migrations vs Hand-Written SQL
Two Ways to Change a Table
A migration is a schema change saved as a file, kept in git with the code that needs it, and run in order on every database. (Git 101 covers the git half.) There are two styles:
| EF migration | Hand-written SQL | |
|---|---|---|
| Who writes it | EF compares your C# classes to its last snapshot | you, or AI, writes the script |
| Good at | tables and columns that follow the code | procedures, views, data moves, tricky indexes |
| Knows what ran | __EFMigrationsHistory table |
your own tracking, or a tool |
| Main risk | you run it without reading it | it drifts away from the code |
Pick one owner for the schema. If EF owns it, nobody changes tables by hand. If SQL scripts own it, EF's classes follow the database. Two owners means one of them is wrong.
The EF Loop
dotnet ef migrations add AddOrderChannel
dotnet ef migrations script PreviousMigration AddOrderChannel
dotnet ef migrations script --idempotent -o migrate.sql
- The first writes the migration file (C#).
- The second prints the actual SQL from one migration to the next. Use your real migration names. Read this.
- The third makes a script that checks the history table and skips what already ran. Review it, then run that on production.
dotnet ef database update applies straight from your machine. Great for your laptop. For production, run a script somebody read.
EF quietly does one thing your hand-written SQL doesn't: it indexes foreign keys by default. SQL Server doesn't. Write REFERENCES dbo.Orders (OrderId) by hand and you get the rule without the index.
EF's default is a plain index on OrderId and nothing else. In a test database, pulling one order's three lines read 6,328 pages with no index on OrderId, 12 with that plain index, and 3 with a covering index, which you'd have to add yourself.
And this catches a model change somebody forgot to make a migration for. Put it in your build:
dotnet ef migrations has-pending-model-changes
The Migration That Eats Data
Rename a property from Name to FullName and EF writes a clean RenameColumn. That's safe for the data. It isn't safe for a running app: code that still asks for Name breaks the moment it's applied. The next lesson covers renaming on a live table.
Do the same rename and change the length from 100 to 150 in the same step, and EF writes this:
migrationBuilder.DropColumn(
name: "Name",
table: "Customers");
migrationBuilder.AddColumn<string>(
name: "FullName",
table: "Customers",
type: "nvarchar(150)",
maxLength: 150,
nullable: false,
defaultValue: "");
Read it. It deletes every customer's name and adds an empty column. It runs perfectly. EF did warn that the operation "may result in the loss of data," in one line of build output that's easy to scroll past.
Here's what it should say:
migrationBuilder.RenameColumn(
name: "Name",
table: "Customers",
newName: "FullName");
migrationBuilder.AlterColumn<string>(
name: "FullName",
table: "Customers",
type: "nvarchar(150)",
maxLength: 150,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(100)",
oldMaxLength: 100);
Fix the Up method and the Down method both. Editing a migration is normal. It's a draft until it's been applied.
In any migration, AI-written or EF-generated, search for Drop before you do anything else. Every hit needs a reason you can say out loud.
You renamed a property and changed its length in one step. What might EF generate?