在ASP.NET核心中的实体框架中更新数据库的开发和生产环境

发布于 2025-02-03 09:57:06 字数 1464 浏览 5 评论 0原文

如何将EF迁移应用于多个数据库,并根据需要引用开发环境或生产环境?

tl; dr

我正在使用实体框架开发ASP.NET Core MVC应用程序,该应用程序引用了不同的数据库进行开发和生产。两个数据库均托管Azure SQL数据库。

目前,该应用程序正在开发机上的IIS Express上运行,但是我将生产将生产部署到Azure App Service。我正在使用实体框架和迁移来更新数据库设计。

我已经应用了此答案使用其他数据库连接字符串进行开发或生产启动配置,这似乎是在工作。然后将连接字符串存储在appSettings.development.jsonappsettings.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江挽川 2025-02-10 09:57:06

根据 Documentation ,您可以将连接字符串指定为额外参数:

- 连接:数据库的连接字符串。默认为AddDbContext或OnConfiguring中指定的默认值。

因此,此命令将使您指定您要定位的数据库:

dotnet ef database update --connection "<sql connection string>"

According to the documentation, you can specify the connection string as an extra parameter:

--connection: The connection string to the database. Defaults to the one specified in AddDbContext or OnConfiguring.

So this command will let you specify which database you are targeting:

dotnet ef database update --connection "<sql connection string>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文