ASP.NET Core提供了一种方便的方式来在不同的环境中使用不同的配置。我们可以使用appsettings.json文件来配置不同环境所使用的数据库。以下是一个示例:
{
"ConnectionStrings": {
"Development": "Server=(local);Database=MyDatabaseDev;Trusted_Connection=True;",
"Production": "Server=myServerAddress;Database=MyDatabaseProd;User Id=myUsername;Password=myPassword;"
}
}
public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("Development");
services.AddDbContext(options => options.UseSqlServer(connectionString));
}
$env:ASPNETCORE_ENVIRONMENT = "Development" // 开发环境
$env:ASPNETCORE_ENVIRONMENT = "Production" // 生产环境
使用这种方法,我们可以轻松地在不同的环境中使用不同的数据库,而无需手动更改代码。
上一篇:ASP.NETCore6ODataSwaggerUI总是显示$count查询
下一篇:ASP.NetCore6RazorPagesHowdoyoucreateanAntiPermissionPolicy