mrahhal / Migrator.EF6

.NET Core CLI tool to enable EF6 migrations in an Asp.Net Core app.
MIT License
82 stars 15 forks source link
aspnetcore ef6 ef6-migrations

Migrator.EF6

Build status NuGet version License

.NET Core CLI tool to enable EF6 migrations in an Asp.Net Core app (RC2 and onwards).

Looking for .net core 3 support?

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.

Looking for the project.json version?

Checkout the preview2 tree version of this repository.

Getting EF6 migrations to work

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".

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.

More commands

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.

If you're working with Identity 3.0

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

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.

Release notes

The 2.1.* releases align with .NET Core SDK 2.1.

2.1.0

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

1.0.5

1.0.4

1.0.3

1.0.2

1.0.1

1.0.0

The 1.0.0-rc2* releases align with .NET Core RC2.

1.0.0-rc2-3

1.0.0-rc2-2

1.0.0-rc2