Enable Entity Framework Core (1.1.1) Migrations From Separate Assembly

Watch the video if you don't like reading:

So, today I tried updating all my project packages to dotnet core 1.1.1. Everything went pretty well except the EF Core's 1.1.1 update. Like everyone else I like putting my DbContext and Repository files in a seperate assembly. A barebone DbContext class may look like this:

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
}

Up until 1.1.0 I was able to run migrations. But today when I tried to add a new migration I got the following error message:

No parameterless constructor was found on 'ApplicationDbContext'. Either add a parameterless constructor to 'ApplicationDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationDbContext'.

So, that was new I thought! First I added a parameterless constructor which of course didn't work. Then I searched the internet to get some knowledge about the IDbContextFactory. Here is the link where you can get some ideas on Using IDbContextFactory

Following the approach I did add a ApplicationDbContextFactory class and implemented the IDbContextFactory just like below:

class ApplicationDbContextFactory : IDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext Create(DbContextFactoryOptions options)
    {
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=erp_db;Trusted_Connection=True;MultipleActiveResultSets=true");

        return new ApplicationDbContext(optionsBuilder.Options);
    }
}

Again tried adding a migration and no luck! Now this error pops up:


Notice that I'm running my migrations against the assembly where my DbContext file is. But turns out EF Core wont let me do this anymore. It requires me to run migrations in the project where I configured Entity Framework to use as a service. In my case I have a web project and in its startup.cs I have configured it like the following:

services.AddDbContext<ApplicationDbContext>(o =>
  o.UseSqlServer(Configuration.GetConnectionString("LocalDBConnection")));

The last piece of the puzzle is, even though I changed my Default Project to my project where the Entity Framework service is configured. Its showing me another error:

Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("ERP")). By default, the migrations assembly is the assembly containing the DbContext.

So just like what the message said. I went to my ApplicationDbContextFactory class and add the assembly after the connection string (b=>b.MigrationsAssembly("ERP")):

public ApplicationDbContext Create(DbContextFactoryOptions options)
{
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=erp_db;Trusted_Connection=True;MultipleActiveResultSets=true", b=>b.MigrationsAssembly("ERP"));

        return new ApplicationDbContext(optionsBuilder.Options);
  }

Now adding new migrations are working just like before.

Oh another thing, the migration histories are now stored in the project that was set by default in the package manager console. If you want to store them in your class library project, change the assembly name both in Startup.cs and ApplicationDbContext i.e. b=>b.MigrationsAssembly("ERP.Data")

Kindly add the command for changing the location where the migration files should go in the comment if you know it. Cause actually I also need it :)