Skip to content

tuespetre/Migrator.EF6

 
 

Repository files navigation

Migrator.EF6

Build status NuGet version License

DNX command line tool to enable EF6 migrations in an Asp.Net Core app.

Getting EF6 migrations to work

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 target frameworks.
    • Remove everything EF7 and add Migrator.EF6 + EF6 to your dependencies. 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 the commands section.
  • Inside Startup.cs:

    • Remove the line of code that starts with services.AddEntityFramework completely (this belong to EF7). Also remove serviceScope.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>();
  • Replace all Microsoft.AspNet.Identity.EntityFramework usings with MR.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.

More commands

These commands do not exist in the normal migrator:

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 RC1

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

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.

About

DNX command line tool to enable EF6 migrations in an Asp.Net Core app

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.0%
  • PowerShell 1.0%