gpt4 book ai didi

docker - 无法创建 'DatabaseContext' 类型的对象。对于设计时支持的不同模式错误

转载 作者:行者123 更新时间:2023-12-04 18:31:21 28 4
gpt4 key购买 nike

我在 ubuntu os 的 docker 中使用 sqlserver2017
创建 asp.netcore 3 网络应用程序
运行 dotnet ef add migrations firstmigrate 时出错
无法创建“DatabaseContext”类型的对象。对于设计时支持的不同模式,
image1
image2

最佳答案

在添加带有 (2) 参数的自己的 DbContext 构造函数后有类似的问题。应用程序正常,但迁移停止工作。
你看起来很像 - 如果您不需要 DatabaseContext 构造函数,请尝试将其删除 ...
有一个构建和 DI 在幕后运行。
在我的情况下修复:

  • 使用来自 Dotnet tool @xspdf 的信息更新 EntityFrameworkCore(3.1.5 出于奇怪的原因,但解决方案有 5)
    dotnet 工具更新--global dotnet-ef
  • 删除了我的新 DbContext 构造函数,并使用另一个答案中的 Config 类替换了 OnConfiguring 中的硬编码连接字符串 here .

  • 此命令在 cmd 中添加迁移构建/运行期间可以显示最多。
    在我的情况下,注意当前目录是(VS)启动项目的 Migrations 文件夹。
    dotnet ef --startup-project ../ --verbose migrations add test
    3.1.5 & 上下文激活错误
    The Entity Framework tools version '3.1.5' is older than that of the runtime '5.0.0'. Update the tools for the latest features and bug fixes.
    Finding DbContext classes...
    Finding IDesignTimeDbContextFactory implementations...
    Finding application service provider in assembly '...'...
    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 '...Context'.
    Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type '...Context'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
    ---> System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate '...'. (my additional parameter)
    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.<FindContextTypes>b__13()
    --- End of inner exception stack trace ---
    at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13()
    at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
    at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
    Unable to create an object of type '...Context'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

    关于docker - 无法创建 'DatabaseContext' 类型的对象。对于设计时支持的不同模式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64763935/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com