.NET Core CLI tool to enable EF6 migrations in an Asp.Net Core app (RC2 and onwards).
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:
- Inside your csproj:
<PackageReference Include="Migrator.EF6.Tools" Version="1.1.3" PrivateAssets="All" />
<DotNetCliToolReference Include="Migrator.EF6.Tools" Version="1.1.3" />
-
Inside
Startup.cs
:- Remove everything EF Core related.
- Simply add your db context to services:
services.AddScoped<ApplicationDbContext>();
-
Replace all
Microsoft.AspNetCore.Identity.EntityFramework
usings withMR.AspNet.Identity.EntityFramework6
if you're using Identity 3.0 (check out the section below). -
Remove the "Migrations" or the "Data/Migrations" folder that EF Core generated.
-
Finally:
dotnet ef migrations enable dotnet ef migrations add InitialCreate dotnet ef 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:
Reverts a number of migrations. database update ~
will revert one migration, and database update ~2
will revert 2 migrations.
Truncates all tables in the database. This is basically 'database update 0'.
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.
A basic sample that shows how to add Migrator.EF6.Tools
to your project.json
.
A sample using Migrator.EF6
and MR.AspNet.Identity.EntityFramework6
to enable EF6 + migrations + Identity 3.0 in your Asp.Net Core app.
I'm aware that I should have aligned the version of Migrator.EF6.Tools
to the dotnet tooling instead of aligning it to the runtime release cycle (I kinda did that without thinking about the whole thing). But, what's done is done and I doubt that the tooling will change that much from now on so all is good for now.
The 1.1.*
releases align with .NET Core tooling 1.0
.
- Fix supporting multiple contexts. #42
- Better error reporting when mistyping context names. #38
- Only handle exceptions we know about. Let others bubble up.
- Remove unrelated exception messages about "project.json".
- Samples: move models and migrations to separate class library.
This release is for tooling 1.0
and VS 2017 support.
The 1.0.*
releases align with .NET Core tooling 1.0.0-preview2
.
This release is for .Net Core 1.1.0
- Fixed: Embed the "Source" resource when it's available. #30
- Support overriding the connection string from the command line through the
-cs
option. This way you won't have to hard code the string inside theDbContext
. #28
- Support multiple
DbContext
s in the same project by using the-c
option to specify which one to target. #27
- Only look for constructable
DbContext
s andDbMigrationsConfiguration
s. #25 - Use
MigrationsDirectory
inDbMigrationConfiguration
if it's available and no directory is specified. #26
- Allow relative database updates to migrations using "~". So
database update ~
will revert one migration, anddatabase update ~2
will revert 2 migrations.
database update
now has a--force
option to ignore possible data loss while updating the database.
- Added an output directory option for migrations. #24
migrations enable
now automatically finds the app's DbContex type name to use in the "Configuration.cs" generated file.
- Initial release supporting .NET Core
1.0.0
.
The 1.0.0-rc2*
releases align with .NET Core RC2
.
- Added support for the following commands:
migrations script
: Generate a SQL script from migrations.
- Fixed: calling the tool required building the project before invocations. Now the tool automatically builds the target project so that it's always up to date. #11
- Initial release supporting .NET Core RC2.