簡體   English   中英

未安裝 Microsoft.EntityFrameworkCore.Design

[英]Microsoft.EntityFrameworkCore.Design is not installed

我正在嘗試使用 EntityFramework Core 和 PM 控制台運行 Add-Migration 命令(代碼優先方法)。 我無法克服此錯誤:無法執行此命令,因為未安裝 Microsoft.EntityFrameworkCore.Design。 安裝與 Microsoft.EntityFrameworkCore 已安裝版本匹配的包版本,然后重試。 這是我的 project.json 文件:

    {
  "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%" ]
  }
}

如您所見,我的依賴項和工具是 1.1.0 版。 這些是我的參考: 參考

我嘗試過的事情:

  • 關閉/重新打開 Visual Studio
  • dotnet 還原
  • 我的啟動文件在 ConfigServices 中注冊了我的連接字符串

我已經安裝了我應該安裝的所有東西(我知道)。 我還在 StackOverflow 上查看了有關此問題的其他帖子,但無濟於事。

任何人都能夠通過這個? 我很感激任何幫助。

首先,修復netcoreapp1.0"Microsoft.NETCore.App": "1.1.0"之間的不一致"Microsoft.NETCore.App": "1.1.0"

其次,您需要Microsoft.EntityFrameworkCore.Tools來運行 PM-Console 命令。

現有的Microsoft.EntityFrameworkCore.Tools.DotNet用於 dotnet CLI 命令。 您可以從命令行創建運行dotnet ef migrations add <name>

有關使用 PM 和/或 CLI 的詳細信息在這里

還有另一種方法可以做到這一點(.Net Core 3.1):

我們需要使用 DbContext 的工廠來在設計時創建遷移。

我們實現 IDesignTimeDbContextFactory 接口,因為按照慣例,當在 DbContext 同一個項目或啟動項目中發現實現該接口的類時,EFCore 會放棄其他創建 DbContext 的方式,將使用工廠。

見實現示例:

  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);
    }

實現后,將包含 DbContext 的項目設置為啟動和默認項目,並嘗試創建遷移:

//.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM