ubuntu 无法创建“DatabaseContext”类型的对象,对于设计时支持的不同模式,出现错误

svgewumm  于 2022-12-11  发布在  其他
关注(0)|答案(3)|浏览(187)

我正在使用sqlserver2017在docker在ubuntu操作系统创建asp.netcore 3网络应用程序错误在运行dotnet ef添加迁移firstmigrate
无法创建“DatabaseContext”类型的对象。对于设计时支持的不同模式,

dldeef67

dldeef671#

在添加自己的DbContext构造函数(带2个参数)后出现类似问题。应用程序正常,但迁移停止工作。
看起来很相似-如果不需要DatabaseContext构造函数,请尝试将其删除...
有一个构建和DI在后台运行。
修复我的案例:
1.使用Dotnet tool @xspdf中的信息更新 EntityFrameworkCore(出于奇怪的原因使用了3.1.5,但解决方案中有5)

. net工具更新--全局. net-ef

1.删除了我的新DbContext构造函数,并使用此处另一个答案中的Config类替换了OnConfiguring中的硬编码连接字符串。
此命令可以在cmd中添加迁移构建/运行期间显示最多内容。
在我的例子中,记住当前目录是启动项目的Migrations文件夹。

dotnet ef --startup-project ../ --verbose migrations add test

3.1.5上下文激活错误(& C)

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
qgelzfjb

qgelzfjb2#

将启动项目设置为已注册的项目“Application DbContext”

h22fl7wq

h22fl7wq3#

通常,当您在项目中使用多上下文时,会引发此错误。要解决此问题,您应该按如下所示修改program.cs:为每个上下文输入以下代码

builder.Services.AddDbContext<Context1>(options =>
    options.UseSqlServer(connectionString));

    builder.Services.AddDbContext<Context2>(options =>
    options.UseSqlServer(connectionString));

相关问题