无法创建类型为 '[DBContext名称]' 的对象。针对设计时所支持的不同模式进行操作。

158

我正在Udemy上学习Mosh Hamedani的ASP.NET MVC课程。

在使用代码优先(Entity Framework)设计我的数据库时,我遇到了一个错误。

起初,我遇到了"未在程序集中找到DbContext"的错误。解决了这个问题后,另一个错误突然出现了。

下面的图片将展示您在添加迁移时发现的错误。我已经搜索过相同的错误,但徒劳无功。我已经苦苦挣扎了两个小时,但到目前为止什么都没解决。

请有人帮帮我。谢谢

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


在添加自己的DbContext构造函数(带有两个参数)后出现了类似的问题。应用程序是正常的,但迁移停止工作。 通过使用Dotnet工具@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

2
是的,现在它正在工作。需要将一段C#代码添加到启动文件中。 - OoMaR jOhUr
16
@OoMaRjOhUr,您可以添加一个回答,展示您添加的代码以使其正常工作,并接受该答案。这可能会帮助未来的某个人。 - tomRedox
尝试在调试模式下运行应用程序,看看在dbcontext被注入到di容器之前应用程序是否会抛出异常。我曾经遇到过同样的问题,是由于应用程序在"options.UseSqlServer(.)"之前抛出异常引起的。希望这可以帮到你。 - Dennis Kabugua
1
在 Program.cs 文件中,在 builder.Services.AddDbContext 代码行之后添加 "var app = builder.Build();" 这一行。 - Davud Seyfullayev
将所有从 AddSingleton 和 AddScoped 注册的服务临时重新注册为 AddTransient。这对我起作用了。 - Stefan Vukovic
显示剩余3条评论
12个回答

1
在我的情况下,我使appsettings.development.json文件成为必需的,但我没有创建该文件。如果您不创建特定于环境的设置文件,请将其设置为optional:true
var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();

但是,需要注意的是错误可能与DbContext及其配置无关。它可能是应用程序启动中的其他错误,导致EF工具无法执行迁移。为了定位项目启动中的任何其他错误,您可以在Startup的各个行添加控制台消息。您甚至可以在尝试迁移时使用控制台检查appsetting值,例如connectionstrings。

 Console.WriteLine("I can see this because the app was able to proceed this far."); 

0

你真的链接了一个该死的西班牙语版本吗? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接