【问题标题】:EF Core migration error with .NET Core Web API in Visual Studio CodeVisual Studio Code 中 .NET Core Web API 的 EF Core 迁移错误
【发布时间】:2020-07-24 13:51:36
【问题描述】:

我在 Visual Studio Code 中有两个项目的解决方案:

  • webapi 项目名为 Sample.API
  • classlib 名为 Sample.Data 的项目

在 Sample.Data 中,我安装了这些包:

Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools

为简单模型创建 DbContext 并指定连接字符串后,我在执行此命令时遇到以下错误(在 Sample.Data 文件夹中):

dotnet ef migrations add InitialMigration

Startup project 'Sample.Data.csproj' targets framework '.NETStandard'.
There is no runtime associated with this framework, and projects targeting it can't be executed directly.
To use the Entity Framework Core .NET Command-line Tools with this project, add an executable project
targeting .NET Core or .NET Framework that references this project, and set it as the startup project
using --startup-project; or, update this project to cross-target .NET Core or .NET Framework...

如果这是 Visual Studio,我会将 WebAPI 项目设置为启动项目。但我正在使用 VS Code。

我错过了什么?

【问题讨论】:

  • 您是否尝试过使用选项 dotnet ef migrations add InitialMigration --startup-project Sample.API.csproj ?这应该将项目设置为启动link

标签: c# .net-core visual-studio-code entity-framework-core asp.net-core-webapi


【解决方案1】:

谢谢@sea_ocean ...你必须指定启动项目:

dotnet ef migrations add MigrationOne --startup-project ../Sample.API/Sample.API.csproj

然后

dotnet ef database update --startup-project ../Sample.API/Sample.API.csproj

您还需要将 Microsoft.EntityFrameworkCore.Design 安装到启动项目中

【讨论】:

    猜你喜欢
    • 2020-09-27
    • 2021-12-19
    • 2018-01-15
    • 2020-08-22
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2021-11-23
    相关资源
    最近更新 更多