Seeding data for a particular environment in Entity Framework Core 2.1

asp.net-core c# entity-framework-core

Question

I'm using the new way of seeding data in EF Core 2.1 and I want to seed data for the development environment only.

Initially I tried this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != EnvironmentName.Development)
    {
        return;
    }

    modelBuilder.Entity<Customer>().HasData(new Customer { ... });
}

However, I noticed that the generated migration will always insert the customers into the database.

Is there a way of restricting the data seed on a per environment basis?

1
2
6/25/2018 3:54:41 PM

Popular Answer

I think you can do in Startup.cs in ConfigureServices method like this

if (env.IsDevelopment())
   // then seed data

Here is recommended way to seed data in Asp.Net Core (Note that I'm not checking environment here). Please look at my sample here

And this file will be use to at run time seed

0
4/14/2019 1:31:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow