9
votes

I am trying to run the command Add-Migration (code first approach) using EntityFramework Core using PM Console. I cannot get past this error: Cannot execute this command because Microsoft.EntityFrameworkCore.Design is not installed. Install the version of that package that matches the installed version of Microsoft.EntityFrameworkCore and try again. This is my project.json file:

    {
  "dependencies": {
    "Microsoft.NETCore.App": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": "1.1.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.1.0-preview4-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },
  "runtimes": {
    "win10-x64": {}
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

As you can see my dependencies and tools are version 1.1.0. These are my references: References

Things I have tried:

  • Close/reopen Visual Studio
  • dotnet restore
  • My startup file has my connection string registered in ConfigServices

I have everything installed that I supposed to (that I am aware of). I have also looked at other posts for this problem on StackOverflow to no avail.

Anyone able to get past this? I appreciate any help.

2
Do you tried reinstal .Net Core SDK? microsoft.com/net/download/core#/currentJ. Doe
You are targeting netcoreapp1.0 but you reference the "Microsoft.NETCore.App": "1.1.0" meta package. Try targeting netcoreapp1.1 or use the older meta packageTseng
@Tseng I updated this. Now I get this error: 'Add-Migration' is not recognized as the name of a cmdlet... I have closed and reopened VS a couple of times. Same error: 'Add-Migration' is not recognized as the name of a cmdlet...EB.

2 Answers

15
votes

First, fix inconsistency between netcoreapp1.0 and "Microsoft.NETCore.App": "1.1.0"

Second, you need Microsoft.EntityFrameworkCore.Tools for running PM-Console commands.

Existing Microsoft.EntityFrameworkCore.Tools.DotNet is for dotnet CLI commands. You may create migrations running dotnet ef migrations add <name> from command line.

Details about using PM and/or CLI is here

1
votes

There's another way to do this (.Net Core 3.1):

We need to use a factory for the DbContext to create migration in design time.

We implement IDesignTimeDbContextFactory interface, because, for convention, when a class that implements this interface is found in the DbContext same project or in startup project, EFCore discard other ways to create the DbContext and will use the factory.

See the implementation example:

  public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
  {

    public MyDbContext CreateDbContext(string[] args)
    {
      var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();

      optionsBuilder.UseSqlServer("Your ConnectionString Here");

      return new MyDbContext(optionsBuilder.Options);
    }

After implement this, set the project that contains the DbContext as startup and default project and try to create the migrations:

//.Net Core CLI:
dotnet ef migrations Add Initial -p .\src\Data\Data.csproj -s .\src\Data\Data.csproj

//Package Manager Console
Add-Migration Initial -p .\src\Data\Data.csproj -s .\src\Data\Data.csproj