无法创建类型为"[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


使用(2)参数添加自己的DbContext构造函数后,出现类似问题.应用程序还可以,但是迁移已停止.已通过使用 Dotnet工具提供的信息,通过第1次更新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项目设置为设置为启动项目

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

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

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天全站免登陆