简体   繁体   中英

Entity Framework Core - No design-time services were found

I have a pretty basic migration file. I'm executing dotnet ef database update --verbose in the Package Manager Console window and nothing is getting generated in SQL Server.

The final lines of output in the Package Manager Console window is shown below:

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.

Here is what my code looks like and adding and removing migrations work. It's just trying to update the database that I am having this issue.

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) { }
}

I also have the following nuget packages installed for the project and both dlls are in my bin directory: Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.Design

As referenced in this article you should add the design time DbContext. Add the following class to your project:

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);
        }
    }
}

After adding this class, EF CLI will use it for design time database creation and updates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM