You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Mediator模式时Dotnet EF migrations add命令无法运行求助

解决EF Core Code First迁移在分层架构中的报错问题

启动项目为Web.UI时的报错处理

报错信息:

Your startup project 'Web.UI' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again

处理步骤:

  • 切换到Web.UI项目目录,执行命令安装依赖包:
    dotnet add package Microsoft.EntityFrameworkCore.Design
  • 保持Web.UI为启动项目,执行迁移命令时指定DbContext所在的Infrastructure项目:
    dotnet ef migrations add initialCreate --project Infrastructure

启动项目为Infrastructure时的报错处理

报错信息:

The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was Infrastructure/appsettings.json

推荐解决方案(无需切换启动项目):
直接在终端执行迁移命令时,同时指定启动项目和目标项目:
dotnet ef migrations add initialCreate --project Infrastructure --startup-project Web.UI
这种方式无需复制配置文件,避免重复维护。

备选方案(不推荐):

  • 将Web.UI中的appsettings.json复制到Infrastructure项目内
  • 右键该文件,设置「复制到输出目录」为「如果较新则复制」

分层架构下EF迁移的最佳实践

  • 固定以Web.UI作为启动项目,它包含完整的配置和依赖注入启动逻辑
  • 执行迁移时通过--project参数指定DbContext所在项目,无需频繁切换启动项目
  • 确保Web.UI项目已安装Microsoft.EntityFrameworkCore.Design,这是EF工具运行的核心依赖

内容的提问来源于stack exchange,提问作者Rahul Parab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 00:42:05