DNX command line tool to enable EF6 migrations in an Asp.Net Core app.
Note: make sure you have the following nuget feed configured if dependencies are not being resolved:
https://www.myget.org/F/aspnetmaster/api/v3/index.json
In visual studio you can go to Tools → Options
and then NuGet Package Manager → Package Sources
to configure feeds.
Steps needed (nothing hard, just a lot of inital steps that you'll have to do one time):
-
Inside
project.json
:- Remove
dnxcore50
from the targetframeworks
. - Remove everything
EF7
and addMigrator.EF6
+EF6
to yourdependencies
. In your dependencies section:
- "EntityFramework.Commands": "7.0.0-rc1-final", - "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final", + "EntityFramework": "6.1.3", + "Migrator.EF6": "1.1.0",
"ef": "EntityFramework.Commands"
→"ef": "Migrator.EF6"
in thecommands
section.
- Remove
-
Inside
Startup.cs
:- Remove the line of code that starts with
services.AddEntityFramework
completely (this belong to EF7). Also removeserviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate()
if it exists.
- services.AddEntityFramework()... - serviceScope.ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate()
- Simply add your db context to services:
services.AddScoped<ApplicationDbContext>();
- Remove the line of code that starts with
-
Replace all
Microsoft.AspNet.Identity.EntityFramework
usings withMR.AspNet.Identity.EntityFramework6
if you're using Identity 3.0 (check out the section below). -
Remove the Migrations folder that EF7 generated.
-
Finally:
dnx ef migrations enable dnx ef migrations add InitialCreate dnx ef database update
You might have to edit the db context's name after enabling migrations if there are errors, so do that before going on.
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 AppDbContext() : 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["Data:DefaultConnection:ConnectionString"];
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 EF7, 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:
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 EF7 one).
Samples are in the samples/
directory. Watch out for MNOTE:
occurrences for notes.
A sample using Migrator.EF6
and MR.AspNet.Identity.EntityFramework6
to enable EF6 + migrations + Identity 3.0 in your Asp.Net Core app.