Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 无法创建类型为';[DBContext';的名称]';。用于设计时支持的不同模式_Asp.net_Asp.net Mvc_Entity Framework_Entity Framework Core_Entity Framework Migrations - Fatal编程技术网

Asp.net 无法创建类型为';[DBContext';的名称]';。用于设计时支持的不同模式

Asp.net 无法创建类型为';[DBContext';的名称]';。用于设计时支持的不同模式,asp.net,asp.net-mvc,entity-framework,entity-framework-core,entity-framework-migrations,Asp.net,Asp.net Mvc,Entity Framework,Entity Framework Core,Entity Framework Migrations,我正在学习Udemy中关于ASP.NETMVC的Mosh Hamedani课程之一 我在使用代码优先(实体框架)设计数据库时遇到一个错误 首先,我得到一个错误,“在程序集中找不到DbContext”。在解决了这个问题之后,另一个问题不知从何而来 下图将显示添加迁移时发现的错误。我已经找过同样的错误,但没有找到。在过去的两个小时里我一直在努力,但到现在为止什么都没有解决 求求你,有人,帮帮我。谢谢 无法创建“Vidly_Context”类型的对象。有关设计时支持的不同模式,请参见 在使用(2)个

我正在学习Udemy中关于ASP.NETMVC的Mosh Hamedani课程之一

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

首先,我得到一个错误,“在程序集中找不到DbContext”。在解决了这个问题之后,另一个问题不知从何而来

下图将显示添加迁移时发现的错误。我已经找过同样的错误,但没有找到。在过去的两个小时里我一直在努力,但到现在为止什么都没有解决

求求你,有人,帮帮我。谢谢

无法创建“Vidly_Context”类型的对象。有关设计时支持的不同模式,请参见


在使用(2)个参数添加自己的DbContext构造函数后出现类似问题。应用程序正常,但迁移停止工作。 通过第一次更新EF(3.1.5在使用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
实体框架工具版本“3.1.5”比运行时版本“5.0.0”旧。更新工具以获得最新功能和错误修复。
正在查找DbContext类。。。
正在查找IDesignTimeDbContextFactory实现。。。
正在程序集“…”中查找应用程序服务提供程序。。。
正在查找Microsoft.Extensions.Hosting服务提供商。。。
在类“Program”上未找到静态方法“CreateHostBuilder(字符串[])”。
找不到应用程序服务提供程序。
正在项目中查找DbContext类。。。
找到DbContext“…Context”。
Microsoft.EntityFrameworkCore.Design.OperationException:无法创建“…上下文”类型的对象。有关设计时支持的不同模式,请参见https://go.microsoft.com/fwlink/?linkid=851728
--->System.InvalidOperationException:尝试激活“…”时无法解析类型“System.String”的服务。(我的附加参数)
位于Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider提供程序)
位于Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider提供程序,类型instanceType,对象[]参数)
位于Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider提供程序,类型)
在Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.c__DisplayClass13_4.b_13()中
---内部异常堆栈跟踪的结束---
在Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.c__DisplayClass13_4.b_13()中
位于Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1工厂)
位于Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(字符串contextType)
位于Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(字符串名称、字符串输出目录、字符串上下文类型、字符串命名空间)
位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(字符串名称、字符串输出目录、字符串上下文类型、字符串命名空间)
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.c__DisplayClass0_0.b_0()中
在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_0`1.b__0()中
位于Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(操作)
无法创建类型为“…上下文”的对象。有关设计时支持的不同模式,请参见https://go.microsoft.com/fwlink/?linkid=851728

在包含MVC项目和Blazor项目的.NET Core 3解决方案中使用EF Core code first迁移时,我遇到了这个问题

如果将解决方案的启动项目设置为Blazor项目,则在使用
添加迁移时会出现错误,但如果将启动项目设置为MVC项目,则不会出现错误


通过阅读并比较这两个项目的startup.cs文件中的代码,我不确定为什么会这样,但暂时将启动项目切换到MVC项目会为我解决问题。

1-确保将您的web项目设置为设置为启动项目

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


3-然后再次运行该命令

我遇到了相同的问题。OP提到他们需要在Startup.cs中添加代码

上有以下代码示例

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
        => services.AddDbContext<ApplicationDbContext>();
}
公共类启动
{
public void配置服务(IServiceCollection服务)
=>services.AddDbContext();
}
我的代码在ConfigureServices方法中缺少以下内容:

services.AddDbContext<ApplicationDbContext>();
services.AddDbContext();

在为我的数据库上下文添加此项后,问题已得到解决。

在启动文件配置服务方法中

services.AddDbContextPool<contextName>(
                    options => options.UseSqlServer(Configuration.GetConnectionString("conString")));
services.AddDbContextPool(
options=>options.UseSqlServer(Configuration.GetConnectionString(“conString”));

我出现此错误是因为我的appsettings.json中缺少逗号

{
  "ConnectionStrings": {
    "DefaultCoonection": "Data Source=Leonardo-Notebook;Initial Catalog=MyDB;Integrated Security=True"
  } <- Comma missing here
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
{
“连接字符串”:{
“DefaultCooConnection”:“数据源=Leonardo笔记本;初始目录=MyDB;集成安全性=True”

}错误消息表示无法在设计时创建上下文。如果在迁移命令中使用--startup project标志(或-s)指定启动项目,则可以帮助命令在设计时创建上下文实例,方法与在运行时创建上下文实例的方法类似

例如:

cd ./project_with_migrations_folder
dotnet ef --startup-project ../my_startup_project_path/ migrations add myMigration01

这是您在使用ASP.NET Core时遇到的最无意义的问题之一。我对您提到的课程不太清楚,但如果您的应用程序与我的应用程序一样,将表示层与数据层分离,请确保安装最新版本的Microsoft.EntityFrameworkCore.Design
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MyNamespace
{
    public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
    {
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .Build();

            var optionsBuilder = new DbContextOptionsBuilder();

            var connectionString = configuration
                        .GetConnectionString("DefaultConnection");

            optionsBuilder.UseSqlServer(connectionString);

            return new ApplicationDbContext(optionsBuilder.Options);
        }
    }
}
var applicationDbContextFactory = new ApplicationDbContextFactory();

using (var dbContext = applicationDbContextFactory.CreateDbContext(args))
{
    
}
var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();
 Console.WriteLine("I can see this because the app was able to proceed this far.");