Learn Entity Framework Core: https://www.learnentityframeworkcore.com/
Entity Framework Core Tutorial: https://entityframeworkcore.com/
Entity Framework 6 Tutorial: https://entityframework.net/
Info
Version: | 8.0.5 |
Author(s): | Ionut Neagos |
Last Update: | Sunday, May 19, 2024 |
.NET Fiddle: | Create the first Fiddle |
Project Url: | https://github.com/ionutneagos/efcore-automaticmigrations |
NuGet Url: | https://www.nuget.org/packages/EFCore.AutomaticMigrations |
Install
Install-Package EFCore.AutomaticMigrations
dotnet add package EFCore.AutomaticMigrations
paket add EFCore.AutomaticMigrations
EFCore.AutomaticMigrations Download (Unzip the "nupkg" after downloading)
Dependencies
- Microsoft.CodeAnalysis.CSharp(>= 3.8.0)
- Microsoft.EntityFrameworkCore.SqlServer(>= 5.0.0)
Tags
How to use:
/// Migrations Options
public class DbMigrationsOptions
{
/// <summary>
/// Allow auto migration with data lost
///
</summary>
public bool AutomaticMigrationDataLossAllowed { get; set; } = false;
/// <summary>
/// Enable to execute auto migration
///
</summary>
public bool AutomaticMigrationsEnabled { get; set; } = true;
/// <summary>
/// Drop all tables from database and recreate based on model snapshot.
Useful in scenarios when the data is transient and can be dropped when the schema changes. For example during prototyping, in tests, or for local changes.
///
</summary>
public bool ResetDatabaseSchema { get; set; } = false;
/// <summary>
/// Allow to update snapshot.The key will pe replaced with value for each KeyValuePair
///
</summary>
public Dictionary<![CDATA[string, string]]> UpdateSnapshot { get; set; } = new Dictionary<![CDATA[string, string]]>;
}
Option 1 (using method):
MigrateDatabaseToLatestVersion.Execute(context);
MigrateDatabaseToLatestVersion.Execute(context, new DbMigrationsOptions { ResetDatabaseSchema = true });
Option 2 (using context extension)
context.MigrateToLatestVersion();
context.MigrateToLatestVersion(new DbMigrationsOptions { ResetDatabaseSchema = true });.