有很多可能性
- 预处理器指令
就像你和 Gert Arnold 已经讨论过的一样,使用 #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
}
- 配置
另一种方法是在 appsettings.json 中进行配置,也许您想使用开发数据设置应用程序,您可以添加类似
{ "environment" : "development" }
并在种子中检查以下内容:
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" },
);
}
}
- 环境变量(asp net core的解决方案)
(另见https://docs.asp.net/en/latest/fundamentals/environments.html)
可以添加环境变量
之后通过 DI:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
SeedDataForDevelopment();
}
}