asp.net - 无法创建类型为 '[DBContext' s Name]' 的对象。对于设计时支持的不同模式

标签 asp.net asp.net-mvc entity-framework entity-framework-core entity-framework-migrations

关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

10 个月前关闭。




Improve this question




我正在 Udemy 中学习有关 ASP.NET MVC 的 Mosh Hamedani 类(class)之一。
我在使用代码优先( Entity Framework )设计数据库时遇到了一个错误。
起初,我得到了 的错误“在程序集中找不到 DbContext” .解决了这个问题后,又一个突然冒出来了。
下图将显示添加迁移时发现的错误。我已经搜索了相同的错误,但没有找到。我在过去的两个小时里一直在挣扎,但到目前为止还没有解决任何问题。
请有人帮助我。感谢

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 tool @xspdf 的信息第一次更新 EF(使用 5 时出于奇怪的原因使用 3.1.5)修复如果未设置,则通过方法+硬编码的默认连接字符串替换提到的构造函数。
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 & 上下文激活错误
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 项目设置为 设为启动项目

2- 在 包管理器控制台 ,将您的数据访问层(如果有)设置为默认项目

3-然后再次运行命令

关于asp.net - 无法创建类型为 '[DBContext' s Name]' 的对象。对于设计时支持的不同模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55123853/

相关文章:

c# - Entity Framework 4 获取插入记录的主键 ID

C# - 在三元运算符中呈现部分

用于密码存储的 asp.net keystore ?

asp.net - 如果成功插入数据库,如何从 ASP.NET Web 服务传回参数? ASP.NET/jQuery/AJAX/JSON

jquery - Json:使用 JQuery 解析以 HTML 形式显示

c# - 使用 Code First Entity Framework 的数据访问层、模型层和 POCO 类

c# - "The type or namespace name ' 使用 ' could not be found"路由 "attribute routing"

c# - 'Microsoft.ACE.OLEDB.12.0' 提供程序未在本地机器(服务器)上注册

asp.net-mvc - ReturnUrl QueryString 在 MVC 4 中始终为空

c# - CSharpProvider 运行时编译找不到 DLL