.NET核心EF dbContext迁移不起作用
因此,您正在使用干净的体系结构进入代码优先的EF设计。您在.infra或.data项目中定义的.CORE项目中的模型。 转到开发人员命令
dotnet ef migrations add myDBInit --context MyDbContext --output-dir Migrations
提示
您准备创建迁移并 > 在类'program'上没有找到静态方法“ create -hostbuilder(string [])'。
找不到申请服务提供商。
在项目中查找dbContext类...
找到了dbContext'mydbContext'。
Microsoft.EntityFrameWorkCore.Design.OperationException:无法创建类型“ myDbContext”的对象。有关设计时间支持的不同模式,请参见 https://go.microsoft.com/microsoft.com/fwlink /?linkID = 851728
---> system.invalidoperationException:无法解析'Microsoft.entityFrameWorkCore.dbContextOptions`1 [caseEscalation.infra.mydbcontext]''''' 在microsoft.extensions.ipendentiondoction.activatorities.constructormatcher.createinstance(iserviceProvider提供商)
在microsoft.extensions.dependentive.activatorities.createinstance上 在Microsoft.extensions.dependentiondoction.activatorities.getServServiceOrcreateinStance(IserviceProvider提供商,类型)
在Microsoft.entityframeworkcore.design.internal.dbcontextoperations
。
So, there you are venturing into code-first EF design with Clean Architecture. You have your models in .Core project, db context defined in the .Infra or .Data project. You getting ready to create the migrations and go to the Developer Command Prompt, dial to the infra folder and type out
dotnet ef migrations add myDBInit --context MyDbContext --output-dir Migrations
And then suddenly you see this error....
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'MyDBContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'MyDBContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[CaseEscalation.Infra.MyDBContext]' while attempting to activate 'CaseEscalation.Infra.MyDBContext'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.b__13()
What do you do!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在互联网上的许多不同解决方案中,最好的解决方案是:您需要一个临时的DB上下文工厂。尤其是正确的(根据少数人),当您处于干净的拱形场景中,program.cs不是项目的一部分。将以下代码粘贴到DBContext,这将告诉命令提示如何处理此上下文!迁移将通过现在。
Among many different solutions on the internet, the best one is : You need a Temporary DB Context Factory. Especially true (according to few folks) when you are in a clean arch scenario where Program.cs is not part of the project. Paste the below code to the DBContext and this will tell the command prompt how to handle this context!! Migrations will go through now.