Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different seed for development and production

What is the recommended approach to seed a database differently using Entity Framework (6+) depending on the build configuration (Debug / Release)?

Right now I am using the MigrateDatabaseToLatestVersion initializer. During development I like to have fake data in my database for testing. So I create this test data in the Seed method of the Configuration class (that came with enabling code-first). However, each time I publish the product via build server I have to comment a lot of code inside my seed method, commit this, create the release, and then undo all the comments to continue development with the test data.

I guess this is not the way to go. So I hope you can tell me the proper way to do it.

like image 493
Sam Avatar asked Aug 24 '16 19:08

Sam


People also ask

What is the development of seed?

The launch of seed development in flowering plants (angiosperms) is initiated by the process of double fertilization: two male gametes (sperm cells) fuse with two female gametes (egg and central cell) to form the precursor cells of the two major seed components, the embryo and endosperm, respectively.

What is the importance of seeds in crop production give at least 5 reasons?

Seeds are of immense biological and economic importance. They contain high protein, starch and oil reserves that help in the early stages of growth and development in a plant. These reserves are what make many cereals and legumes major food sources for a large proportion of the world's inhabitants.


1 Answers

there are many possibilities

  1. Preprocessor directive

One is like you and Gert Arnold already talked about, using the #if DEBUG:

protected override void Seed(BookService.Models.BookServiceContext context)
{
#if DEBUG
    context.Authors.AddOrUpdate(x => x.Id,
        new Author() { Id = 1, Name = "Test User" },
    );
#else
    context.Authors.AddOrUpdate(x => x.Id,
        new Author() { Id = 1, Name = "Productive User" },
    );
#endif
}
  1. Configuration

Another way would be with configuration in the appsettings.json, maybe you want to set up the application with development-data, you can add something like

{ "environment" : "development" }

and in the seed you check for this:

protected override void Seed(BookService.Models.BookServiceContext context)
{
    var builder = new ConfigurationBuilder();
    builder.AddInMemoryCollection();
    var config = builder.Build();

    if (config["environment"].Equals("development"))
    {
        context.Authors.AddOrUpdate(x => x.Id,
            new Author() { Id = 1, Name = "Test User" },
        );
    }
    else if (config["environment"].Equals("producion"))
    {
        context.Authors.AddOrUpdate(x => x.Id,
            new Author() { Id = 1, Name = "Productive User" },
        );
    }
}
  1. Environment variables (solution for asp net core)

(see also https://docs.asp.net/en/latest/fundamentals/environments.html)

You can add an environment variable

enter image description here and later on via DI:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        SeedDataForDevelopment();
    }
}
like image 180
Matthias Burger Avatar answered Nov 18 '22 08:11

Matthias Burger