Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 833
scooter Avatar asked Nov 04 '20 07:11

scooter


People also ask

What is idesigntimeservices in EF Core?

These services are managed separately from EF Core's runtime services to prevent them from being deployed with your app. To override one of these services (for example the service to generate migration files), add an implementation of IDesignTimeServices to your startup project.

Why can't I reference entityframeworkcore types in another project?

Microsoft.EntityFrameworkCore.Design is a DevelopmentDependency package. This means that the dependency won't flow transitively into other projects, and that you cannot, by default, reference its types. In order to reference its types and override design-time services, update the PackageReference item's metadata in your project file.

Why configurationmanager is not working in EF Core?

The problem is that something changed between EF Core 2.2 to 3.1 that has broken or prevented ConfigurationManager from initializing properly. Now we must take extra steps to open the configuration for app.config and web.config.

Why do I need a derived dbcontext in EF Core Tools?

Privacy policy. Thank you. Some of the EF Core Tools commands (for example, the Migrations commands) require a derived DbContext instance to be created at design time in order to gather details about the application's entity types and how they map to a database schema.


1 Answers

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

like image 165
Sergey Anisimov Avatar answered Sep 18 '22 08:09

Sergey Anisimov