【问题标题】:Entity Framework Core - No design-time services were foundEntity Framework Core - 未找到设计时服务
【发布时间】:2021-02-16 21:51:44
【问题描述】:

我有一个非常基本的迁移文件。我正在包管理器控制台窗口中执行dotnet ef database update --verbose,SQL Server 中没有生成任何内容。

Package Manager Console 窗口中的最后几行输出如下所示:

Finding design-time services for provider Microsoft.EntityFrameworkCore.SqlServer...
Using design-time services from provider Microsoft.EntityFrameworkCore.SqlServer.
Finding design-time services referenced by assembly BM.Server.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly BM.Server...
No design-time services were found.
Done.

这是我的代码的样子,添加和删除迁移工作。它只是试图更新我遇到此问题的数据库。

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<BMDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BMDbConnectionString")));
    }
}

public class BMDbContext : DbContext
{
    public BMDbContext(DbContextOptions<BMDbContext> options) : base(options) { }
}

我还为该项目安装了以下 nuget 包,并且两个 dll 都在我的 bin 目录中: Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Design

【问题讨论】:

标签: c# entity-framework-core


【解决方案1】:

正如this article 中提到的,您应该添加设计时 DbContext。将以下类添加到您的项目中:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace WebApplication8
{
    public class DesignTimeBMDbContext : IDesignTimeDbContextFactory<BMDbContext>
    {
        public BMDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<BMDbContext>();
            // pass your design time connection string here
            optionsBuilder.UseSqlServer("<connection_string>");
            return new BMDbContext(optionsBuilder.Options);
        }
    }
}

添加此类后,EF CLI 将使用它来创建和更新设计时数据库

【讨论】:

  • 请注意,该命名空间应与您项目的命名空间匹配
猜你喜欢
  • 2018-03-20
  • 1970-01-01
  • 2021-03-23
  • 2022-08-20
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多