无法创建类型为“[DBContext 的名称]"的对象.对于设计时支持的不同模式 [英] Unable to create an object of type '[DBContext's Name]'. For the different patterns supported at design time

本文介绍了无法创建类型为“[DBContext 的名称]"的对象.对于设计时支持的不同模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Udemy 中学习有关 ASP.NET MVC 的 Mosh Hamedani 课程之一.

I'm following one of Mosh Hamedani Course on ASP.NET MVC in Udemy.

我在使用代码优先(实体框架)设计数据库时遇到了一个错误.

I came across one error while designing my Database using code-first (Entity Framework).

起初,我得到了"的错误;在程序集中找不到 DbContext".解决了这个问题后,又突然冒出一个.

At first, I got the error of " No DbContext was found in assembly". After resolving this problem another one surged from nowhere.

下图将显示添加迁移时发现的错误.我已经搜索了相同的错误,但没有找到.过去两个小时我一直在挣扎,但直到现在都没有解决.

The image below will show you the error found while adding a migration. I've already searched for the same error but in vain. I'm struggling for the past two hours but nothing is solved till now.

请有人帮助我.谢谢

无法创建类型为Vidly_Context"的对象.有关设计时支持的不同模式,请参阅 https://go.microsoft.com/fwlink/?linkid=851728

unable to create an object of type 'Vidly_Context'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728


使用 (2) 参数添加自己的 DbContext 构造函数后出现类似问题.应用程序正常,但迁移停止工作.通过使用来自 Dotnet 工具的信息第一次更新 EF(使用 5 时出于奇怪的原因使用 3.1.5)修复@xspdf 并通过方法 + 硬编码的默认连接字符串替换提到的构造函数(如果未设置).


Similar problem after adding own DbContext constructor with (2) parameters. App was ok, but migrations stopped working. Fixed by 1st updating EF (3.1.5 used for strange reason when working with 5) using info from Dotnet tool @xspdf and replacing mentioned constructor by method + hardcoded default connection string if not set.

dotnet tool update --global dotnet-ef

// following command show the most during migration build/run in cmd
// mind current dir is Migrations folder of (VS) startup project here
dotnet ef --startup-project ../ --verbose migrations add test

3.1.5 &上下文激活错误

3.1.5 & context activation Error

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

推荐答案

1- 确保将您的 web 项目设置为 Set as Startup Project

1- Make sure to set your web project as Set as Startup Project

2- 在 Package Manager Console 中,将您的数据访问层(如果有)设置为默认项目

2- In Package Manager Console, set your data access layer (if any) as a default project

3- 然后再次运行命令

3- Then run the command again

这篇关于无法创建类型为“[DBContext 的名称]"的对象.对于设计时支持的不同模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆