.NET核心EF dbContext迁移不起作用

发布于 2025-02-04 15:31:13 字数 1128 浏览 4 评论 0原文

因此,您正在使用干净的体系结构进入代码优先的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百善笑为先 2025-02-11 15:31:13

在互联网上的许多不同解决方案中,最好的解决方案是:您需要一个临时的DB上下文工厂。尤其是正确的(根据少数人),当您处于干净的拱形场景中,program.cs不是项目的一部分。将以下代码粘贴到DBContext,这将告诉命令提示如何处理此上下文!迁移将通过现在。

public class TemporaryDbContextFactory : IDesignTimeDbContextFactory<MyDBContext>
{
    private readonly IConfiguration _configuration;
    public TemporaryDbContextFactory() { }
    public TemporaryDbContextFactory(IConfiguration configuration)
    {
      _configuration = configuration;
    }
    public MyDBContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<MyDBContext>();
        builder.UseSqlServer("server=myserver;database=mydatabase;user=myuser;password=mypassword;MultipleActiveResultSets=true;Connect Timeout=180",
        optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(MyDBContext).GetTypeInfo().Assembly.GetName().Name));
        return new MyDBContext(builder.Options);
    }
}

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.

public class TemporaryDbContextFactory : IDesignTimeDbContextFactory<MyDBContext>
{
    private readonly IConfiguration _configuration;
    public TemporaryDbContextFactory() { }
    public TemporaryDbContextFactory(IConfiguration configuration)
    {
      _configuration = configuration;
    }
    public MyDBContext CreateDbContext(string[] args)
    {
        var builder = new DbContextOptionsBuilder<MyDBContext>();
        builder.UseSqlServer("server=myserver;database=mydatabase;user=myuser;password=mypassword;MultipleActiveResultSets=true;Connect Timeout=180",
        optionsBuilder => optionsBuilder.MigrationsAssembly(typeof(MyDBContext).GetTypeInfo().Assembly.GetName().Name));
        return new MyDBContext(builder.Options);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文