在ASP.NET核心中的实体框架中更新数据库的开发和生产环境
如何将EF迁移应用于多个数据库,并根据需要引用开发环境或生产环境?
tl; dr
我正在使用实体框架开发ASP.NET Core MVC应用程序,该应用程序引用了不同的数据库进行开发和生产。两个数据库均托管Azure SQL数据库。
目前,该应用程序正在开发机上的IIS Express上运行,但是我将生产将生产部署到Azure App Service。我正在使用实体框架和迁移来更新数据库设计。
我已经应用了此答案使用其他数据库连接字符串进行开发或生产启动配置,这似乎是在工作。然后将连接字符串存储在appSettings.development.json
或appsettings.production.json
中。
当我选择开发启动配置时,我可以使用EF CLI命令dotnet EF数据库更新
,以便将我的迁移应用于开发数据库。正常工作。
但是,当我在POR启动配置下运行时,我无法理解如何告诉Visual Studio将每个迁移应用于生产数据库。
运行命令dotnet EF数据库更新
更改启动配置后继续引用开发数据库,而没有任何更改。 我对此并不感到惊讶 - 我还没有告诉Visual Studio在哪里可以找到其他数据库。但是我也无法弄清楚如何做到这一点。
是否有一种方法可以根据启动配置或环境变量更改引用的数据库?还是其他方便的方式?
lainingsettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:57961",
"sslPort": 44320
}
},
"profiles": {
"IIS Express (dev)": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express (prod)": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
How do I apply EF migrations to multiple databases, referencing either a Development or Production environment as required?
TL;DR
I'm developing an ASP.NET Core MVC application using Entity Framework that references different databases for Development and Production. Both databases are hosted Azure SQL databases.
The application at the moment is running on IIS Express on my Development machine, but I will deploy Production to an Azure App Service. I'm using Entity Framework and migrations to update the database design.
I've applied this answer to use a different database connection string for either the Development or Production launch configurations, and this seems to be working. The connection strings are then stored in appsettings.Development.json
or appsettings.Production.json
.
When I select the DEV launch configuration, I am able to use the EF cli command dotnet ef database update
in order to apply my migrations to the development database. Works fine.
However, I cannot work out how to tell Visual Studio to apply each migration to the production database when I run under the PROD launch configuration.
Running the command dotnet ef database update
after changing the launch configuration continues referencing the development database, and nothing changes. I'm not surprised by this - I haven't told Visual Studio where to find the other database. But I also can't work out how to do this.
Is there a way to change the referenced database, based on the launch configuration or environment variables? Or some other convenient way?
launchSettings.json
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:57961",
"sslPort": 44320
}
},
"profiles": {
"IIS Express (dev)": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express (prod)": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 Documentation ,您可以将连接字符串指定为额外参数:
因此,此命令将使您指定您要定位的数据库:
According to the documentation, you can specify the connection string as an extra parameter:
So this command will let you specify which database you are targeting: