Code-first migration How to set default value for new property?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you add a new property to an entity that already has rows in the database, the migration has to answer one practical question: what value should existing rows receive. In Entity Framework migrations, the answer is usually either a database default, a one-time backfill, or both.
The Simplest Case: Add the Column with a Default
In classic Entity Framework code-first migrations, you can set a default directly when adding the column. Example for EF6:
This does two things:
- Existing rows get a value so the migration can succeed.
- Future inserts also use that database default unless the application supplies another value.
That is usually what people want when the default should remain part of the schema.
Using a SQL Expression Instead of a Literal
Sometimes the default should be generated by the database, such as the current timestamp. In EF6 migrations you can use defaultValueSql:
Use this only for SQL expressions the target provider understands. GETUTCDATE() is a SQL Server example, not a cross-database default.
When You Only Want to Backfill Existing Rows
Sometimes the new property needs a value for existing rows, but you do not want a permanent database default for future inserts. In that case, add the column as nullable or with a temporary default, backfill the data, and then enforce the final shape.
Example:
This pattern is often better when the initial value is data migration logic rather than a true schema default.
EF Core Version
If you are using EF Core instead of EF6, the generated migration looks different but the idea is the same:
For a database expression in EF Core:
So the concept is stable even though the migration API differs.
Choosing the Right Strategy
Ask two questions before editing the migration:
- Should future rows still get this default automatically at the database level.
- Is the value really a static default, or is it a one-time backfill for old data.
If the answer to the first question is no, prefer a backfill migration over leaving a permanent default constraint behind. That keeps the schema closer to the real business rules.
Common Pitfalls
- Adding a non-null column without a default or backfill, which causes the migration to fail on existing rows.
- Using
defaultValuewhen the database should calculate the value withdefaultValueSql. - Leaving a permanent default in the schema when it was only needed for old data.
- Assuming SQL expressions are portable across database providers.
- Forgetting to update application code and validation rules after the schema change.
Summary
- A new non-null property needs either a default value or a backfill strategy for existing rows.
- Use
defaultValuefor a literal database default anddefaultValueSqlfor provider-specific SQL expressions. - If the value is only needed for old rows, backfill with SQL and then enforce the final column shape.
- EF6 and EF Core use different migration APIs, but the migration strategy is the same.
- Choose the option that matches the business rule, not just the easiest migration to generate.

