My problem is i am trying to seed an Entity Framework Core database with data and in my mind the below code show work. I\'ve realised that this should not be called in the <
Assuming you are using the built-in DI container, here is one way you can accomplish this.
Reference your seed method in the Configure method of your startup class, and pass the IApplicationBuilder object as a parameter instead of the DbContext, like this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//...
// Put this at the end of your configure method
DbContextSeedData.Seed(app);
}
Next, modify your seed method to accept the IApplicationBuilder instance. Then you'll be able to spin up an instance of the DbContext, and perform your seed operation, like this:
public static void Seed(IApplicationBuilder app)
{
// Get an instance of the DbContext from the DI container
using (var context = app.ApplicationServices.GetRequiredService())
{
// perform database delete
context.Database.EnsureDeleted;
//... perform other seed operations
}
}