我在添加一个初始迁移到.NET核心类库里面我的实体框架数据库上下文问题的迁移。.NET核心实体框架 - 上下文添加在类库
当我运行:
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类库到“应用程序”有静态主要方法。因为我无法再引用Placeholder.Business中的Placeholder.Data,所以我必须使用microsoft doc页面上列出的解决方法2。当我现在运行迁移脚本我得到如下:
No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext
卫生署ofcourse这不会工作,在的DbContext是从我Placeholder.Web应用程序注册(通过业务层)。然后我唯一的选择就是在新的静态main方法添加新的上下文,我真的不希望这样做..
我以后会在我回家时试试这个! – Reft
如果它适合你,请标记为答案。如果您想更进一步,您还可以在DATA类库项目中扩展IServiceCollection,并在其中添加DI和连接字符串,以便您的Web项目完全独立于DATA项目。 – Reza
对不起,延迟回复,只是尝试了你的代码,它的工作原理,欢呼声。但!如果你看看我的项目结构,我有一个名为migrations的自创文件夹。当我运行命令时,我在根目录中得到一个新的迁移文件夹?我能改变这个吗? 2.如果我想在第一次初始迁移后添加更多迁移,我是否需要再次执行这些步骤,或者可以使用包管理器控制台以正常方式创建它? – Reft