【发布时间】:2017-02-13 21:08:09
【问题描述】:
我在将初始迁移添加到 .NET Core 类库中的 Entity Framework 数据库上下文时遇到问题。
当我跑步时:
dotnet ef migrations add migrationName -c PlaceholderContext
我得到错误:
Could not invoke this command on the startup project 'Placeholder.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. See http://go.microsoft.com/fwlink/?LinkId=798221 for details and workarounds.
所以我点击了link 并了解到无法将迁移添加到类库。但是,您可以将类库项目转换为“应用程序”项目,但这样做我无法从我的业务层(类库)引用这个“应用程序”项目。
项目结构:
Placeholder.Web (WebAPI) => Placeholder.Business(类库)=> Placeholder.Data(类库)
Placeholder.Web => Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
//HERE WE REGISTER DB CONTEXT, (STATIC CLASS IN BUSINESS LAYER)
services.InjectBusinessContext(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=Placeholder;Integrated Security=True;Connect Timeout=30;");
services.InjectWebServices();
services.InjectBusinessServices();
}
我该如何克服这个非常烦人的问题?
更新(1)
我已将我的 Placeholder.Data 类库转换为具有静态 main 方法的“应用程序”。因为我不能再从 Placeholder.Business 引用 Placeholder.Data,所以我必须使用 microsoft doc 页面上列出的解决方法 2。当我现在运行迁移脚本时,我得到以下信息:
没有为此 DbContext 配置数据库提供程序。一种 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序 方法或通过在应用程序服务提供者上使用 AddDbContext。 如果使用了 AddDbContext,那么还要确保你的 DbContext 类型 接受一个 DbContextOptions 对象在其构造函数中,并将其传递给 DbContext 的基本构造函数
这当然行不通,dbcontext 是从我的 Placeholder.Web 应用程序注册的(通过业务层)。然后我唯一的选择是在新的静态 main 方法中添加一个新的上下文,我真的不想这样做..
【问题讨论】:
标签: c# .net entity-framework asp.net-core .net-core