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.
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.
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.
there are many possibilities
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
}
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" },
);
}
}
(see also https://docs.asp.net/en/latest/fundamentals/environments.html)
You can add an environment variable
and later on via DI:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
SeedDataForDevelopment();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With