【问题标题】:Enable migration for EF Core in class library project在类库项目中启用 EF Core 迁移
【发布时间】:2017-01-24 06:07:52
【问题描述】:

我创建了一个类库项目,在其中定义了我的模型、DbContext、连接字符串等。但是,我无法使用下面的命令添加迁移

命令

dotnet ef migration add initial

我收到以下错误

错误

Could not invoke this command on the startup project 'Alan.Pois.Data'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications.

下面是project.json

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.1",
    "Microsoft.EntityFrameworkCore.Design": { "version": "1.1.0", "type" : "build" },
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.1.0",
    "CommonServiceLocator": "1.3.0"
  },

  "frameworks": {
    "net461": {
    }
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools.DotNet": {
      "version": "1.1.0-preview4"
    }
  }
}

有什么遗漏吗?

问候, 艾伦

【问题讨论】:

  • 我不熟悉 .Net Core 执行此操作的方式,但在经典 .Net 中,如果您尝试启用此功能并且您的启动项目未正确引用您的 DbContext,您会遇到类似的异常.您可以通过将包管理器控制台设置为正确的项目或传递一些额外的参数来解决它。

标签: entity-framework .net-core entity-framework-core


【解决方案1】:

我在类库中有我的上下文、模型和存储库。我的迁移保存在我的 Web 项目中,以便工具工作(我认为最适合您遇到的相同情况的解决方法),只需在我的 EF Core 注册中进行简单的配置调用。不确定这是否适合您...

还有其他方法可以解决这个问题,例如configuring your class library as an application,而且,我意识到您仍然需要按照我的方式在两个项目(在我的情况下是 Web 应用程序和类库)中引用 EF 包.我还没有完成“将您的类库配置为应用程序”,因为这感觉就像是在提交某些东西。

这是我的Startup.cs 的 EF 设置:

services.AddDbContext<MyContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("MyConnectionString"), b => b.MigrationsAssembly("MyStartupProject")));

【讨论】:

  • 你能把它上传到 git 上吗?我想看看例子。谢谢
猜你喜欢
  • 2018-11-30
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2016-09-19
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
相关资源
最近更新 更多