The Wild

Preview EF Core 10 Named Default Constraint Migrations

· 6 min read · Mammals

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to preview the SQL that Entity Framework Core 10 will generate for migrations that add or modify named default constraints, so you can review and validate changes before deploying them to a production database. You'll generate a migration script and inspect it without touching your live database.

What You'll Need

  • A .NET project using EF Core 10 (or a recent preview) with an existing DbContext and entity models.
  • The EF Core command-line tools installed (either the dotnet-ef global tool or the Package Manager Console in Visual Studio).
  • Access to a development database (or a connection string that points to a test instance) so you can generate the migration script against a real schema.
  • Basic familiarity with the command line and the structure of EF Core migrations.

Step-by-Step

  1. Check your EF Core version
    Run dotnet ef --version in your project directory. Make sure you have EF Core 10 or a preview that supports named default constraints (introduced earlier but refined in 10). If not, update the Microsoft.EntityFrameworkCore package in your project.
  2. Add a migration for your model changes
    If you haven't already, create a migration that includes the default constraint changes. Use dotnet ef migrations add YourMigrationName (or Add-Migration in Package Manager Console). This generates a migration file in your project's Migrations folder.
  3. Open the generated migration file
    Look at the Up and Down methods. You'll see calls like AlterColumn or AddColumn with a defaultValue or defaultValueSql parameter. In EF Core 10, you can also specify a name for the constraint using the defaultValueSql or a new HasDefaultValue fluent API with .HasName().
  4. Generate a SQL script
    Use the dotnet ef migrations script command to produce the SQL that will be executed. For example: dotnet ef migrations script -o migration.sql. This creates a file containing the full SQL for all migrations from the last applied one to the latest. If you want only the latest migration, use dotnet ef migrations script PreviousMigrationName YourMigrationName.
  5. Inspect the generated SQL for named constraints
    Open the migration.sql file and search for CONSTRAINT or the name you provided. In EF Core 10, named default constraints appear as CONSTRAINT [DF_TableName_ColumnName] DEFAULT ... in the ALTER TABLE statements. Verify that the names are unique and follow your naming conventions.
  6. Review the script for unintended changes
    Check that the script only includes the expected changes. Pay attention to any DROP CONSTRAINT or ADD CONSTRAINT statements that might affect existing data or indexes. If you see unexpected changes, you may need to adjust your model or migration configuration.
  7. Test the script on a staging database
    Run the script against a copy of your production schema (or a test database) to ensure it executes without errors and produces the desired constraint names. You can use a tool like sqlcmd or your database IDE.
  8. Deploy after verification
    Once you're satisfied with the preview, apply the migration to production using your normal deployment process, or run the script manually if you prefer.

Common Mistakes

  • Forgetting to specify a name for the constraint
    If you don't provide a name, EF Core will generate one automatically (like DF__Table__Column__3B81E5A7), which can be unpredictable. Always use .HasName() in your model configuration to set a meaningful name.
  • Assuming the script is idempotent
    EF Core's migrations script command does not generate idempotent scripts by default. If you run the same script twice, it will fail. Use the --idempotent flag if you need that behavior.
  • Not checking for existing constraint names
    If you reuse a constraint name that already exists in the database, the migration will fail. Always check for naming conflicts before deploying.
  • Overlooking the Down method
    The rollback script is just as important. Make sure the Down method also drops the named constraint correctly, otherwise you'll have orphaned constraints.

Tips

  • Use a naming convention
    Adopt a consistent pattern like DF_TableName_ColumnName for all default constraints. This makes it easier to identify and manage them.
  • Preview with --verbose
    Run dotnet ef migrations script --verbose to see additional details about the operations being performed, which can help you spot issues.
  • Keep migrations small
    If you're making multiple changes, create separate migrations for each logical change. This makes it easier to preview and review the SQL.
  • Use a database diff tool
    For complex changes, compare the generated script against your current schema using a tool like SSDT or a third-party diff tool to catch unexpected differences.
  • Automate the preview
    In your CI/CD pipeline, you can run the migrations script command and store the output as an artifact for review before deployment.

Troubleshooting

  • The script doesn't include the named constraint
    Make sure you've actually configured the name in your model. If you're using data annotations, you can't set a name—you must use the Fluent API in OnModelCreating. Example: entity.Property(e => e.Status).HasDefaultValue("Active").HasName("DF_User_Status").
  • The migration fails with a duplicate constraint name
    Check if the constraint name already exists in the database. You can query the system catalog (e.g., SELECT name FROM sys.default_constraints in SQL Server) to see existing names. Rename your constraint to avoid the conflict.
  • The script is empty
    If you run migrations script and get no output, it might be because there are no pending migrations. Use dotnet ef migrations list to see which migrations are applied and which are pending.
  • Errors about missing migrations
    If you get an error like "No migrations were found", make sure your project builds and that the startup project is set correctly. Use dotnet ef migrations list to diagnose.
  • The script contains unexpected table drops
    This can happen if you have pending model changes that you didn't intend to migrate. Review your model and remove any unintended changes before generating the script.

Frequently Asked Questions

Can I preview the migration without a database connection?

No, the dotnet ef migrations script command requires a database connection to determine the current schema and generate the correct SQL. You can point it to a local test database.

How do I set a custom name for a default constraint in EF Core 10?

Use the Fluent API in your DbContext class: modelBuilder.Entity().Property(e => e.MyProperty).HasDefaultValue(0).HasName("DF_MyTable_MyProperty");

Does the preview show the exact SQL that will run?

Yes, the generated script is the exact SQL that EF Core will execute when you apply the migration. However, it may not include transaction wrapping or other database-specific details.

Can I preview only the latest migration?

Yes, specify the range: dotnet ef migrations script PreviousMigrationName LatestMigrationName. If you omit the first argument, it will include all migrations from the beginning.

What if I need to change the constraint name after generating the migration?

You can edit the migration file directly to change the name, but be careful to also update the Down method. Alternatively, delete the migration and re-add it with the correct name.

Is there a way to see the migration SQL in Visual Studio?

Yes, in Package Manager Console you can use Script-Migration to generate the script. The output can be saved to a file or viewed in the console.