Back to Devexpress

Update Database Schema and Migrations in EF Core

expressappframework-405418-business-model-design-orm-business-model-design-with-entity-framework-core-update-database-and-migrations-in-ef-core.md

latest4.2 KB
Original Source

Update Database Schema and Migrations in EF Core

  • Dec 08, 2025
  • 4 minutes to read

When you develop an application (in Debug mode) you can use the following options to update database schema when the structure of business objects changes:

To update the production database, run the XAF application with specific command line parameters. See the following help topic for additional information: Production Database and Application Updates.

Automatic Database Schema Update

In Debug mode, XAF automatically updates database schema when the structure of business objects changes. For instance, when you add a new business class or a property, or remove a property, XAF can create missing tables, add or remove columns, indexes, etc.

Important

Note that you can lose data if you delete columns, change column types, delete/recreate indexes, or otherwise modify the database.

We recommend that you use automatic database schema update with test databases. Be prepared to lose and recreate data frequently.

In a multi-tenant application, login to every tenant to let XAF automatically update that tenant’s database schema.

Limitations

The database schema update mechanism is based on the standard functionality of EF Core. EF Core limitations are reflected in the update mechanism.

  • If a table contains data, table update can fail. For instance, if you change a column type and it does not match the type of existing data.
  • The XAF automatic update mechanism has the same scenario limitations as the functionality of standard EF Core migrations.
  • If you want to use EF Core migrations, automatic database schema updates cannot be active.

Disable Automatic Updates

You can disable automatic database schema updates for a particular object space provider in ApplicationBuilder:

File: MySolution.Blazor.Server\BlazorApplication.cs or MySolution.Win\Program.cs

csharp
builder.ObjectSpaceProviders
    .AddEFCore(options => options.SchemaUpdateOptions.DisableUpdateSchema = true)
    .WithDbContext<...>(...)
    // ...

EF Core Migrations

Note

If you use EF Core migrations in your project, disable the automatic database schema update since it can disrupt migrations.

Migrations are native to EF Core. They allow you to manually update the database schema and keep it in sync with the application’s data model while preserving existing data.

Before you run the application for the first time, or anytime after you change the data model, you need to generate a migration and then apply it to the database.

Set Up Migrations

Important

Delete the existing database (if there is one) before you proceed, because Entity Framework Core does not take the existing database schema into consideration when it generates the first migration.

  1. Add the Microsoft.EntityFrameworkCore.Tools NuGet package to the YourProjectName.Module project and build the solution.

  2. In the YourProjectName.Module project, go to the BusinessObjects folder and open the YourProjectNameDbContext.cs file. Replace the declaration of the YourProjectNameDesignTimeDbContextFactory class with the following code snippet:

  3. In Visual Studio, open the Package Manager Console and use the following command to add a migration:

  4. Update the database with the following command:

Update the database if you add, rename, or delete a class/property, or if you otherwise change the data model in your application. To do this, repeat steps 3 and 4 of this tutorial. Make sure to use a unique migration name for each new migration.