有很多可能性
- 预处理指令
一种就像你和格特·阿诺德已经讨论过的那样,使用#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 网络核心)
(也可以看看https://docs.asp.net/en/latest/fundamentals/environments.html https://docs.asp.net/en/latest/fundamentals/environments.html)
可以添加环境变量
and later on via DI:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
SeedDataForDevelopment();
}
}