简体   繁体   中英

Microsoft.EntityFrameworkCore.Design is not installed

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: 参考

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM