Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve DbContext in ASP.NET Core 2.0

First of all, I'm trying to seed my database with sample data. I have read that this is the way to do it (in Startup.Configure) (please, see ASP.NET Core RC2 Seed Database)

I'm using ASP.NET Core 2.0 with the default options.

As usual, I register my DbContext in ConfigureServices. But after that, in the Startup.Configure method, when I try to resolve it using GetRequiredService, it throws with this message:

System.InvalidOperationException: 'Cannot resolve scoped service 'SGDTP.Infrastructure.Context.SGDTPContext' from root provider.'

My Startup class like this:

public abstract class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<SGDTPContext>(options => options.UseInMemoryDatabase("MyDatabase"))
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();

        SeedDatabase(app);
    }

    private static void SeedDatabase(IApplicationBuilder app)
    {
        using (var context = app.ApplicationServices.GetRequiredService<SGDTPContext>())
        {
            // Seed the Database
            //... 
        }
    }
}

What am I doing wrong? Also, is this the best place to create seed data?

like image 459
SuperJMN Avatar asked Sep 05 '17 22:09

SuperJMN


People also ask

How does DbContext work in .NET core?

A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns. Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance.

Is DbContext scoped or Singleton?

The AddDbContext extension method registers DbContext types with a scoped lifetime by default.

Should you use using with DbContext?

EF and EF Core DbContext types implement IDisposable . As such, best practice programming suggests that you should wrap them in a using() block (or new C# 8 using statement). Unfortunately, doing this, at least in web apps, is generally a bad idea.

What is DbContext pooling?

Context pooling works by reusing the same context instance across requests; this means that it's effectively registered as a Singleton, and the same instance is reused across multiple requests (or DI scopes).


2 Answers

You're registering SGDTPContext as a scoped service and then attempting to access it outside of a scope. To create a scope inside your SeedDatabase method, use the following:

using (var serviceScope = app.ApplicationServices.CreateScope()) {     var context = serviceScope.ServiceProvider.GetService<SGDTPContext>();      // Seed the database. } 

Credit to @khellang for pointing out the CreateScope extension method in the comments and to @Tseng's comment and answer re how to implement seeding in EF Core 2.

like image 141
Kirk Larkin Avatar answered Oct 14 '22 02:10

Kirk Larkin


Was getting this error while following the official ASP.Net MVC Core tutorial, in the section where you are supposed to add seeded data to your application. Long story short, adding these two lines

 using Microsoft.EntityFrameworkCore;  using Microsoft.Extensions.DependencyInjection; 

to the SeedData class solved it for me:

using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq;  namespace MvcMovie.Models { public static class SeedData {     public static void Initialize(IServiceProvider serviceProvider)     {         using (var context = new MvcMovieContext(             serviceProvider.GetRequiredService<DbContextOptions<MvcMovieContext>>()))         {             // Look for any movies.             if (context.Movie.Any())             {                 return;   // DB has been seeded             }   ... 

Can't tell you the WHY, but these were two of the options I got from following the Alt + Enter quick fix option.

like image 20
Ojonugwa Jude Ochalifu Avatar answered Oct 14 '22 02:10

Ojonugwa Jude Ochalifu