Scaffold-DbContext上的項目中未安裝“Microsoft.EntityFrameworkCore.Tools”

asp.net-core c# entity-framework-core

我按照這裡列出的教程:

https://docs.efproject.net/en/latest/platforms/aspnetcore/existing-db.html

但是,我不想在WebApplication項目中包含DB上下文,而是希望DB Context,Entities等能夠存在於另一個.NET Core類庫中。

通過更新庫project.json文件以包含'netcoreapp1.0'框架,我解決了一些早期的兼容性問題。

project.json

之前:

{
    "version": "1.0.0-*",

    "dependencies": {
        "NETStandard.Library": "1.6.0"
    },

    "frameworks": {
        "netstandard1.6": {
            "imports": "dnxcore50"
        }
    }
}

後:

{
  "version": "1.0.0-*",

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [ "portable-net451+win8" ],
      "buildOptions": {
        "emitEntryPoint": true
      },

      "dependencies": {
        "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
        "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        },
        "Microsoft.EntityFrameworkCore": "1.0.0-*",
        "Microsoft.EntityFrameworkCore.Tools": "1.0.0-*"
      },
      "tools": {
        "Microsoft.EntityFrameworkCore.Tools": "1.0.0-*"
      }
    },
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  }
}

本教程繼續說,為了“逆向工程模型”,必須在包管理器控制台中運行以下命令:

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

我收到以下錯誤消息:

Cannot execute this command because 'Microsoft.EntityFrameworkCore.Tools' is not installed in project 'src\DB'. Add 'Microsoft.EntityFrameworkCore.Tools' to the 'tools' section in project.json. See http://go.microsoft.com/fwlink/?LinkId=798221 for more details.

其中DB是類庫的名稱。

您可以在工具部分清楚地看到Microsoft.EntityFrameworkCore.Tools。所以我不確定如何繼續。

一般承認的答案

project.json模式中定義的“框架”部分中沒有工具部分。

這個應該正常工作

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.0-*",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-*"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-*"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [ "portable-net451+win8" ],
      "buildOptions": {
        "emitEntryPoint": true
      },
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        }
      }
    },
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  }
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因