EFCore.AutomaticMigrations 10.0.0

dotnet add package EFCore.AutomaticMigrations --version 10.0.0
                    
NuGet\Install-Package EFCore.AutomaticMigrations -Version 10.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFCore.AutomaticMigrations" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EFCore.AutomaticMigrations" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EFCore.AutomaticMigrations" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EFCore.AutomaticMigrations --version 10.0.0
                    
#r "nuget: EFCore.AutomaticMigrations, 10.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EFCore.AutomaticMigrations@10.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EFCore.AutomaticMigrations&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=EFCore.AutomaticMigrations&version=10.0.0
                    
Install as a Cake Tool

About the package

.NET 10.0 EF Core 10.0

Enable Automatic Migrations for Entity Framework Core for SQL Databases without manual migration files..

Usage

The package support following ways to apply/view-applied migrations:

Context methods

  • Execute(TContext context);
  • Execute(TContext context, TMigrationsOptions options);
  • ExecuteAsync(TContext context, CancellationToken cancellationToken);
  • ExecuteAsync(TContext context, TMigrationsOptions options, CancellationToken cancellationToken);

Extensions methods

  • MigrateToLatestVersion<TContext, TMigrationsOptions>(this TContext context);
  • MigrateToLatestVersion<TContext, TMigrationsOptions>(this TContext context, TMigrationsOptions options);
  • MigrateToLatestVersionAsync<TContext, TMigrationsOptions>(this TContext context, CancellationToken cancellationToken);
  • MigrateToLatestVersionAsync<TContext, TMigrationsOptions>(this TContext context, TMigrationsOptions options, CancellationToken cancellationToken);

DbMigrationsOptions object allows to configure migration options:

/// <summary>
/// Configuration options for automatic Entity Framework Core migrations.
/// </summary>
/// <remarks>
/// This class provides settings to control how automatic migrations are executed,
/// including data loss protection, schema reset capabilities, and snapshot management.
/// </remarks>
public class DbMigrationsOptions
{
   /// <summary>
   /// Gets or sets a value indicating whether automatic migrations that could result in data loss are allowed.
   /// </summary>
   /// <value>
   /// <see langword="true"/> if data loss is allowed during migrations; otherwise, <see langword="false"/>.
   /// Default is <see langword="false"/>.
   /// </value>
   /// <remarks>
   /// When set to <see langword="false"/>, migrations that could potentially cause data loss 
   /// (such as dropping columns or tables) will throw an exception.
   /// </remarks>
   public bool AutomaticMigrationDataLossAllowed { get; set; } = false;

   /// <summary>
   /// Gets or sets a value indicating whether automatic migrations are enabled.
   /// </summary>
   /// <value>
   /// <see langword="true"/> if automatic migrations are enabled; otherwise, <see langword="false"/>.
   /// Default is <see langword="true"/>.
   /// </value>
   public bool AutomaticMigrationsEnabled { get; set; } = true;

   /// <summary>
   /// Gets or sets a value indicating whether to reset the database schema by dropping all tables and recreating them.
   /// </summary>
   /// <value>
   /// <see langword="true"/> to reset the database schema; otherwise, <see langword="false"/>.
   /// Default is <see langword="false"/>.
   /// </value>
   /// <remarks>
   /// <para>
   /// When set to <see langword="true"/>, all existing tables will be dropped and recreated based on the current model.
   /// This is useful during development, testing, or when working with transient data that can be safely discarded.
   /// </para>
   /// <para>
   /// <strong>Warning:</strong> This will result in complete data loss. Use with caution in production environments.
   /// </para>
   /// </remarks>
   public bool ResetDatabaseSchema { get; set; } = false;

   /// <summary>
   /// Gets or sets a dictionary of key-value pairs for updating the model snapshot.
   /// </summary>
   /// <value>
   /// A dictionary where keys are replaced with their corresponding values in the snapshot.
   /// Default is an empty dictionary.
   /// </value>
   /// <remarks>
   /// This property allows for dynamic modification of the generated model snapshot code
   /// by performing string replacements during the migration process.
   /// </remarks>
   public Dictionary<string, string> UpdateSnapshot { get; set; } = [];

   /// <summary>
   /// Gets or sets the database schema name for the migration history table.
   /// </summary>
   /// <value>
   /// The schema name for the migration table, or <see langword="null"/> to use the model's default schema.
   /// </value>
   /// <remarks>
   /// <para>
   /// If not specified, the migration history table will be created in the same schema as defined by the model.
   /// For SQL databases, this defaults to the "dbo" schema.
   /// </para>
   /// <para>
   /// This property is particularly useful in multi-tenant scenarios or when working with specific database schemas.
   /// </para>
   /// </remarks>
   public string Schema { get; set; }
}
Migration code
 // Get context
 var app = builder.Build();

// begin apply automatic migration database to latest version
await using AsyncServiceScope serviceScope = app.Services.CreateAsyncScope();

await using DbContext? dbContext = serviceScope.ServiceProvider.GetService<DbContext>();

if (dbContext is not null)
{
   // If the database context was successfully resolved from the service provider, we apply migrations.
   // The DbMigrationsOptions object is used to configure automatic data loss prevention and offers other tools like viewing raw SQL scripts for migrations.

     // we can list migration operations as raw sql before applying them and save them to a file or logs
     var migrationsToApply = await dbContext.ListMigrationOperationsAsRawSqlAsync(cancellationToken: CancellationToken.None);

     foreach (var migrationOperation in migrationsToApply.OrderBy(x => x.Order))
     {
         logger.LogInformation("Migration to apply: {SQL}. Is Destructive Change: {ISDESTRUCTIVECHANGE}.", migrationOperation.SqlCommand, migrationOperation.IsDestructiveChange);
     }

   // The database is created automatically if it does not exist, if exist will be updated to latest model changes
   // Pay attention if you are using a PaaS database, like Azure; it will be created automatically using the default SKU and this might affect your costs.

   var migrationOptions = new DbMigrationsOptions
   {
       AutomaticMigrationDataLossAllowed = true,
   };

   //execute migration to latest version
   await dbContext.MigrateToLatestVersionAsync(migrationOptions);

   //at this stage dabatabase containse latest changes
}

Helpers methods

  • ListAppliedMigrationsAsync(context, options, cancellationToken) - list applied migration
  • ListMigrationOperationsAsRawSqlAsync(context, options, cancellationToken) - view the raw SQL being executed, which can log or save if needed.
/// <summary>
/// List applied migration
/// </summary>
/// <returns>List of applied migration</returns>
List<MigrationRaw> ListAppliedMigrations();
async Task<List<MigrationRaw>> ListAppliedMigrationsAsync(CancellationToken cancellationToken = default);


/// <summary>
/// Use this method to list migration operations which will be applied as raw sql
/// </summary>
/// <returns>List of sql scripts. Empty list if no pending migrations, or database is not connected/created</returns>
List<MigrationOperationRaw> ListMigrationOperationsAsRawSql();
async Task<List<MigrationOperationRaw>> ListMigrationOperationsAsRawSqlAsync(CancellationToken cancellationToken = default);
// begin apply automatic migration database to latest version
await using AsyncServiceScope serviceScope = app.Services.CreateAsyncScope();

await using TodoDbContext? dbContext = serviceScope.ServiceProvider.GetService<TodoDbContext>();

if (dbContext is not null)
{
    // List migration operations as raw SQL commands
    var sqlMigrationOperations = await dbContext.ListMigrationOperationsAsRawSqlAsync();

    foreach (var sqlMigrationOperation in sqlMigrationOperations)
    {
        // log sql commands
        Console.WriteLine(sqlMigrationOperation.SqlCommand);
    }

    // Apply migrations
    await dbContext.MigrateToLatestVersionAsync();

    // List applied migrations
    List<MigrationRaw> appliedMigrations = await dbContext.ListAppliedMigrationsAsync();
    foreach (MigrationRaw migration in appliedMigrations)
    {
        Console.WriteLine(migration.MigrationId);
    }
}
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on EFCore.AutomaticMigrations:

Package Downloads
Dao.LightFramework

Package Description

TCDev.APIGenerator.Data.SQL

Adds necessary functionality to use MS SQL Server and Azure SQL Instances with the API Generator.

TCDev.APIGenerator.Data.SQLite

Adds necessary functionality to use SQLLite with the API Generator. Rasepi greates fully working CRUD Apis from just models

TCDev.APIGenerator.Data.Postgres

Adds necessary functionality to use Postgres databases with the API Generator. Rasepi greates fully working CRUD Apis from just models

Tools.Webkit

Webkit adds rudimentary quality-of-life improvements to generic C# and ASP.NET development with tools such as logging extensions, mocking, email system and security.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EFCore.AutomaticMigrations:

Repository Stars
DeeJayTC/net-dynamic-api
A library that turns your model into a fully working API, define your model as markdown (soon), json or c#.
Version Downloads Last Updated
10.0.0 111 1/18/2026
9.0.0 6,802 2/7/2025
8.0.5 23,012 5/19/2024
8.0.0 12,362 11/15/2023
7.0.11 1,134 10/2/2023
7.0.10 2,785 8/20/2023
7.0.7 1,218 6/14/2023
7.0.5.2 2,168 4/28/2023
7.0.5.1 671 4/27/2023
7.0.5 829 4/12/2023
7.0.4 1,069 3/27/2023
7.0.3 725 3/27/2023
7.0.2 742 3/27/2023
7.0.1 1,144 3/27/2023
7.0.0 4,758 12/4/2022
6.0.10.1 11,663 10/20/2022
6.0.10 1,607 10/12/2022
6.0.10-rc 677 10/12/2022
6.0.9.1 921 10/12/2022
6.0.8 912 10/12/2022
6.0.7 892 10/12/2022
6.0.6 986 10/12/2022
6.0.5 891 10/12/2022
6.0.4 5,397 4/15/2022
6.0.3.1 2,512 3/22/2022
6.0.3 1,797 3/22/2022
6.0.0.1 1,302 12/6/2021
6.0.0 1,069 12/6/2021
5.0.0 2,093 11/26/2020
1.0.2 1,315 11/26/2020
1.0.1 3,213 5/11/2020
1.0.0 1,190 4/22/2020

Added support for .NET10
     Upgraded compatibility with Microsoft.EntityFrameworkCore 10.0