Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to correctly seed a DbContext using ASP.NET Core 2.0? #2188

Closed
SuperJMN opened this issue Sep 5, 2017 · 7 comments
Closed

How to correctly seed a DbContext using ASP.NET Core 2.0? #2188

SuperJMN opened this issue Sep 5, 2017 · 7 comments

Comments

@SuperJMN
Copy link

SuperJMN commented Sep 5, 2017

Hello, dear friends in ASP.NET!

I haven't found any information and this is a so common task that I'm almost forced to ask you to, please, give a helping hand to people like me, that are trying to get into the ASP.NET Core ship.

What I'm trying to do is to seed my database with sample data. I haven't found a nice place to perform this startup task. However, some people says the Startup.Configure can be a right place.

However, after following the advice, I have faced some problems with the resolution of the DbContext.

This is my Startup class:

    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
                //... 
            }
        }
    }

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.'

I've also posted this as a question in StackOverflow: https://stackoverflow.com/questions/46063945/cannot-resolve-dbcontext-in-asp-net-core-2-0

Big thanks for the help, in advance.

@Tratcher
Copy link
Member

Tratcher commented Sep 5, 2017

@bricelam where is the guidance for moving this to Main?

@bricelam
Copy link
Contributor

bricelam commented Sep 6, 2017

I'm not sure where/if it got documented...

@bricelam
Copy link
Contributor

bricelam commented Sep 6, 2017

@divega might know.

@joeaudette
Copy link

joeaudette commented Sep 6, 2017

I wrote a blog post based on information I found in issues here, can't remember the specific issue where I saw it but my understanding is seeding and running migrations should now be done in Program.cs not in startup. like this:

public class Program
{
    public static void Main(string[] args)
    {
	    var host = BuildWebHost(args);
	
	    using (var scope = host.Services.CreateScope())
	    {
		    var services = scope.ServiceProvider;

		    try
		    {
			    EnsureDataStorageIsReady(services);

		    }
		    catch (Exception ex)
		    {
			    var logger = services.GetRequiredService<ILogger<Program>>();
			    logger.LogError(ex, "An error occurred while migrating the database.");
		    }
	    }

	    host.Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
	WebHost.CreateDefaultBuilder(args)
		.UseStartup<Startup>()
		.Build();

    private static void EnsureDataStorageIsReady(IServiceProvider services)
    {
	    CoreEFStartup.InitializeDatabaseAsync(services).Wait();
	    SimpleContentEFStartup.InitializeDatabaseAsync(services).Wait();
	    LoggingEFStartup.InitializeDatabaseAsync(services).Wait();
    }

}

@joeaudette
Copy link

just found the issue with the guidance forgot I linked it from my post
dotnet/AspNetCore.Docs#3864

@divega
Copy link
Contributor

divega commented Sep 7, 2017

This is the issue I created asking for this to be covered in the 1.x-to-2.x migration documentation:

dotnet/AspNetCore.Docs#4134

@joeaudette feel free to up-vote.

@aspnet-hello
Copy link

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants