Home > OS >  Entity Framework Core - No referenced design-time services were found
Entity Framework Core - No referenced design-time services were found

Time:06-17

I am trying to run 'update-database -verbose' in the Package Manager Console but I am getting the following lines at the end of the output: (nothing is being generated in my SQL server)

Using context 'TutoringContext'.
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 'LakeTutoringWebsite'...
Finding design-time services referenced by assembly 'LakeTutoringWebsite'...
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'LakeTutoringWebsite'...
No design-time services were found.

I see that I can create a DesignTimeDbContextFactory like this: https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli

But the constructor must be parameterless so I can't pass an IConfiguration object to get my connection string. How can I run 'update-database' without hard coding my connection string?

Based on the above link, I thought I would be able to run 'update-database' since I have added my DBContext to services.

I am using dependency injection for my DBContext in my project:

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

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 TutoringContext : IdentityDbContext<IdentityUser>
{
    public DbSet<Comment> Comments { get; set; }

    public TutoringContext(DbContextOptions<TutoringContext> options) : base(options)
    {
    }

}

I am using ASP.NET Core 3.1

I installed System.Configuration.Configuration version 6.0.0 using NuGet.

CodePudding user response:

Firstly, as well as creating an implementation of IDesignTimeDbContextFactory, your project will need to add a reference to package:

Microsoft.EntityFrameworkCore.Design

Normally, hard-coded strings are not so bad in this class because you are just working with a development database. Ideally, you would apply migrations to other databases (staging, beta, production) in code (instead of update-database in command line) at application startup which will use values from your appsettings.json file accessed thru IConfiguration.

However, if you do want to have the development connection string accessed from a json file, this link does a pretty good job of walking you thru it.

  • Related