there are many possibilities
One of you, like Geert Arnold, has already spoken using #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
}
- Configuration
Another way is with customization in appsettings.json, maybe you want to customize the application using development data, you can add something like
{ "environment" : "development" }
and in the seed you check 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" },
);
}
}
- Environment variables (solution for asp net core )
(see also https://docs.asp.net/en/latest/fundamentals/environments.html )
You can add an environment variable
and later through DI:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
SeedDataForDevelopment();
}
}