.NET Core CLI tool to enable EF6 migrations in an Asp.Net Core app (RC2 and onwards).
Check this comment. This tool is now invalid for .net core 3.0 projects and above. You'll want to use the official ef6.exe instead.
Checkout the preview2 tree version of this repository.
You can read the release notes at the end of this file.
IMPORTANT: it's highly recommended that you put your models and migrations in a pure class library project that has no dependnecies on anything aspnetcore related. Apart from being a better design, there's actually a current problem that prevents the tool from working with projects that depend on aspnetcore. And the new tooling in v0.1 fully supports that. For more info check this issue.
Steps:
<PackageReference Include="Migrator.EF6.Tools" Version="2.1.0" PrivateAssets="All" />
<DotNetCliToolReference Include="Migrator.EF6.Tools" Version="2.1.0" />
Note: If you're on 1.0 of dotnet sdk, you might want to use version "1.1.x".
Startup.cs
:
services.AddScoped<ApplicationDbContext>();
Microsoft.AspNetCore.Identity.EntityFramework
usings with MR.AspNet.Identity.EntityFramework6
if you're using Identity 3.0 (check out the section below).Finally:
dotnet ef6 migrations enable
dotnet ef6 migrations add InitialCreate
dotnet ef6 database update
The tool will automatically build your project but if something goes wrong make sure to build manually with dotnet build
.
As a final note, make sure your db context looks like this:
public class ApplicationDbContext : DbContext // Or IdentityDbContext<ApplicationUser> if you're using Identity
{
public static string ConnectionString { get; set; } = "Server=(localdb)\\mssqllocaldb;Database=aspnet5-Web1-8443284d-add8-41f4-acd8-96cae03e401d;Trusted_Connection=True;MultipleActiveResultSets=true";
public ApplicationDbContext() : base(ConnectionString)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
And in Startup.cs
, in Configure
:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Web1.Migrations.Configuration>());
ApplicationDbContext.ConnectionString = Configuration["ConnectionStrings:DefaultConnection"];
This is really important for the following reasons (not really necessary to read):
EF6 migrations implementation can read the connection string from web.config
and that's why in an Asp.Net < 5 app we were able to just specify the connection's name and EF6 would fetch that. In EF Core, migrations know about dependency injection and can instantiate a DbContext
correctly, EF6 just activates the default ctor so we have to provide the connection string there.
These commands do not exist in the normal migrator:
database update ~[number of migrations to revert]
:Reverts a number of migrations. database update ~
will revert one migration, and database update ~2
will revert 2 migrations.
database truncate
:Truncates all tables in the database. This is basically 'database update 0'.
database recreate
:Truncates all tables then updates the database to the latest migration. This is basically a drop then update. Really helpful in development if you find yourself always dropping the database from SQL Server Object Explorer and then reapplying migrations.
Check out MR.AspNet.Identity.EntityFramework6. It enables you to use Identity 3.0 with EF6 (by using an EF6 provider for Identity instead of the EF Core one).
Samples are in the samples/
directory. Watch out for MNOTE:
occurrences for notes.
BasicConsoleApp
A basic sample that shows how to add Migrator.EF6.Tools
to your project.json
.
WithIdentity
A sample using Migrator.EF6
and MR.AspNet.Identity.EntityFramework6
to enable EF6 + migrations + Identity 3.0 in your Asp.Net Core app.
The 2.1.*
releases align with .NET Core SDK 2.1
.
2.1.0
dotnet ef6
because dotnet ef
is now included SDK 2.1.The 2.0.*
releases align with .NET Core SDK 2.0
.
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
This release supports .NET Core SDK 2.0
.
The 1.1.*
releases align with .NET Core SDK 1.0
.
1.1.4
1.1.3
1.1.1
1.1.0
This release is for tooling 1.0
and VS 2017 support.
The 1.0.*
releases align with .NET Core SDK 1.0.0-preview2
.
1.0.8
This release is for .Net Core 1.1.0
1.0.7
1.0.6
-cs
option. This way you won't have to hard code the string inside the DbContext
. #281.0.5
DbContext
s in the same project by using the -c
option to specify which one to target. #271.0.4
DbContext
s and DbMigrationsConfiguration
s. #25MigrationsDirectory
in DbMigrationConfiguration
if it's available and no directory is specified. #261.0.3
database update ~
will revert one migration, and database update ~2
will revert 2 migrations.1.0.2
database update
now has a --force
option to ignore possible data loss while updating the database.1.0.1
migrations enable
now automatically finds the app's DbContex type name to use in the "Configuration.cs" generated file.1.0.0
1.0.0
.The 1.0.0-rc2*
releases align with .NET Core RC2
.
1.0.0-rc2-3
migrations script
: Generate a SQL script from migrations.1.0.0-rc2-2
1.0.0-rc2