C# 无法解析类型为';Microsoft.EntityFrameworkCore.DbContextOptions';

C# 无法解析类型为';Microsoft.EntityFrameworkCore.DbContextOptions';,c#,asp.net-mvc,asp.net-core,dependency-injection,entity-framework-core,C#,Asp.net Mvc,Asp.net Core,Dependency Injection,Entity Framework Core,我正在尝试在ASP.NET核心项目上使用EF Core 5.0访问数据库。对于第一次迁移,我覆盖了DBContext上的onconfig()方法,并成功地更新了数据库 对于第二次迁移,我决定在ASP.NET内核中使用依赖项注入。这是我做的改动 在myStartup.cs中添加了services.AddDbContext 从DBContext中删除了onconfig()方法 运行dotnet ef migrations add Posts后,我遇到以下错误: Microsoft.EntityFra

我正在尝试在ASP.NET核心项目上使用EF Core 5.0访问数据库。对于第一次迁移,我覆盖了DBContext上的
onconfig()
方法,并成功地更新了数据库

对于第二次迁移,我决定在ASP.NET内核中使用依赖项注入。这是我做的改动

  • 在my
    Startup.cs
    中添加了
    services.AddDbContext
  • DBContext
    中删除了
    onconfig()
    方法
  • 运行
    dotnet ef migrations add Posts
    后,我遇到以下错误:

    Microsoft.EntityFrameworkCore.Design.OperationException: 
    Unable to create an object of type 'BlogContext'. 
    For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
    
    如果添加--verbose标志,则得到以下输出:

    Build started...
    dotnet build blog/app/app.csproj /verbosity:quiet /nologo
    
    Build succeeded.
        0 Warning(s)
        0 Error(s)
    
    Time Elapsed 00:00:02.50
    Build succeeded.
    
    Finding DbContext classes...
    Finding IDesignTimeDbContextFactory implementations...
    Finding application service provider in assembly 'app'...
    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 'BlogContext'.
    Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'BlogContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
     ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[app.Data.BlogContext]' while attempting to activate 'app.Data.BlogContext'.
       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()
       
    Unable to create an object of type 'BlogContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
     
    

    Startup
    BlogContext
    都在同一个项目中。

    多亏了Ivan和Kirk在上面的评论,并阅读了整个详细的输出,我解决了这个问题。结果发现我没有遵循
    Program.cs
    中的正确模式

    这些工具首先尝试通过调用Program.CreateHostBuilder()、调用Build()、然后访问Services属性来获取服务提供程序。

    我通过在
    main()
    中移动
    CreateHostBuilder()
    ,重构了原始的
    Program.cs
    ,这破坏了ef核心迁移

    Program.cs
    修改为following后,它将按预期工作

    public class Program
    {
        public static void Main(string[] args)
            => CreateHostBuilder(args).Build().Run();
    
        // EF Core uses this method at design time to access the DbContext
        public static IHostBuilder CreateHostBuilder(string[] args)
            => Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(
                    webBuilder => webBuilder.UseStartup<Startup>());
    }
    
    公共类程序
    {
    公共静态void Main(字符串[]args)
    =>CreateHostBuilder(args.Build().Run();
    //EF Core在设计时使用此方法访问DbContext
    公共静态IHostBuilder CreateHostBuilder(字符串[]args)
    =>Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(
    webBuilder=>webBuilder.UseStartup());
    }
    
    多亏了Ivan和Kirk在上面的评论,并阅读了整个详细的输出,我才发现了问题所在。结果发现我没有遵循
    Program.cs
    中的正确模式

    这些工具首先尝试通过调用Program.CreateHostBuilder()、调用Build()、然后访问Services属性来获取服务提供程序。

    我通过在
    main()
    中移动
    CreateHostBuilder()
    ,重构了原始的
    Program.cs
    ,这破坏了ef核心迁移

    Program.cs
    修改为following后,它将按预期工作

    public class Program
    {
        public static void Main(string[] args)
            => CreateHostBuilder(args).Build().Run();
    
        // EF Core uses this method at design time to access the DbContext
        public static IHostBuilder CreateHostBuilder(string[] args)
            => Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(
                    webBuilder => webBuilder.UseStartup<Startup>());
    }
    
    公共类程序
    {
    公共静态void Main(字符串[]args)
    =>CreateHostBuilder(args.Build().Run();
    //EF Core在设计时使用此方法访问DbContext
    公共静态IHostBuilder CreateHostBuilder(字符串[]args)
    =>Host.CreateDefaultBuilder(args)
    .ConfigureWebHostDefaults(
    webBuilder=>webBuilder.UseStartup());
    }
    
    您的应用程序入口点(
    程序
    类)如何-是否遵循此处描述的模式?如果是,可能是另一个异常或代码没有显示在这里,这是导致它。发布完整的详细输出可能有助于确定原因。嗨@Ivan,是的,我在
    程序
    类中遵循相同的模式。我已经更新了我的问题,包括
    Program.cs
    文件和完整的详细输出。谢谢您没有遵循建议的模式。请注意@IvanStoev链接的示例如何显示EF Core如何使用代码中缺少的
    Program.CreateHostBuilder
    方法。是的,我刚刚注意到详细输出中的错误<代码>在类“Program”上未找到静态方法“CreateHostBuilder(字符串[])”。找不到应用程序服务提供商。我将重构代码以使用原始模式。谢谢@Ivan和Kirk.Yup,如果存在该方法,他们将尝试直接运行该方法,如文档中所述:“工具首先尝试通过调用Program.CreateHostBuilder()获取服务提供程序…”。详细的输出也显示了这一点。快乐的编码。你的应用程序入口点(
    程序
    类)如何?它是否遵循这里描述的模式?如果是,可能是另一个异常或代码没有显示在这里,这是导致它。发布完整的详细输出可能有助于确定原因。嗨@Ivan,是的,我在
    程序
    类中遵循相同的模式。我已经更新了我的问题,包括
    Program.cs
    文件和完整的详细输出。谢谢您没有遵循建议的模式。请注意@IvanStoev链接的示例如何显示EF Core如何使用代码中缺少的
    Program.CreateHostBuilder
    方法。是的,我刚刚注意到详细输出中的错误<代码>在类“Program”上未找到静态方法“CreateHostBuilder(字符串[])”。找不到应用程序服务提供商。我将重构代码以使用原始模式。谢谢@Ivan和Kirk.Yup,如果存在该方法,他们将尝试直接运行该方法,如文档中所述:“工具首先尝试通过调用Program.CreateHostBuilder()获取服务提供程序…”。详细的输出也显示了这一点。快乐编码。