Enable migration for EF Core in class library project

.net-core entity-framework entity-framework-core


I have created a class library project in which I have defined my Models, DbContext, connectionstring and etc. However , I am not able to add migration using the command below


dotnet ef migration add initial

I am getting the error below


Could not invoke this command on the startup project 'Alan.Pois.Data'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications.

Below is the project.json

  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.1",
    "Microsoft.EntityFrameworkCore.Design": { "version": "1.1.0", "type" : "build" },
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0",
    "CommonServiceLocator": "1.3.0"

  "frameworks": {
    "net461": {
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet": {
      "version": "1.1.0-preview4"

IS there anything missing ?

regards, Alan

1/24/2017 6:07:52 AM

Popular Answer

I have my context, models and repositories in a class library. My migrations are kept in my web project for the tooling to work (the workaround that I thought was best for the same situation you are experiencing) with a simple configuration call in my EF Core registration. Not sure if this is an option for you...

There are other ways around this, like configuring your class library as an application, and, I realize you'd still need to reference EF packages in two projects (the web app and class library in my case) with the way I'm doing it. I haven't done the "configure your class library as an app" because it just feels like punching something into submission.

Here's EF setup from my Startup.cs:

services.AddDbContext<MyContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString"), b => b.MigrationsAssembly("MyStartupProject")));
1/24/2017 9:03:10 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow