entity-framework - 无法使用 GitLab CI 脚本中的 EF Core 连接到 AWS 数据库
问题描述
我有一个使用 Visual Studio 2017 模板构建的 AWS 无服务器应用程序 (.NET Core 2.0)。该应用程序使用实体框架代码优先访问托管在 AWS RDS 中的 MSSQL 数据库。
源代码托管在 GitLab.com,我有一个 CI 脚本来构建、测试和部署应用程序。该脚本使用 GitLab.com 提供的 docker 容器运行。作为部署步骤的一部分,我想用任何新的迁移来更新数据库。为此,我使用dotnet ef database update
命令。
问题:
在 CI 脚本中,数据库更新命令失败,表示找不到服务器或无法访问该服务器。但是,如果我从本地计算机运行相同的命令,则命令成功。
脚本输出dotnet版本和dotnet ef版本,和我本地机器上的完全一样。我还为该命令添加了详细输出,以验证它实际上是否正在尝试连接到正确的数据库。
关于问题的原因是什么以及如何解决它的任何建议?
这是我的 CI 脚本:
image: microsoft/dotnet:latest
stages:
- build
- deploy
build:
stage: build
script:
## some build steps
- dotnet build --configuration Release
- cd "../MyWebProject.Tests"
- dotnet test --configuration Release
- cd "../MyWebProject"
- dotnet publish --configuration Release
artifacts:
paths:
- "./MySolution/MyWebProject/bin/Release/netcoreapp2.0/publish/"
expire_in: 1 day
deploy_01_dev:
stage: deploy
script:
- cd "./MySolution/MyWebProject"
- dotnet --version
- dotnet ef --version
- apt-get update
- apt-get -y install zip
- dotnet restore
- AWS_ACCESS_KEY_ID=MY_AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY=MY_AWS_SECRET_ACCESS_KEY dotnet lambda deploy-serverless --package=./bin/Release/netcoreapp2.0/publish/MyWebProject.1.0.0.nupkg
- dotnet ef database update --project ../MyDbProject --verbose
以下是所涉及的 dotnet 版本 - 如前所述,我在本地计算机上获得了相同的版本:
$ dotnet --version
2.1.302
$ dotnet ef --version
Entity Framework Core .NET Command-line Tools
2.1.1-rtm-30846
以下是 CI 输出中最相关的部分:
$ dotnet ef database update --project ../MyDbProject --verbose
...
Finding DbContext classes in the project...
Found DbContext 'MyDbContext'.
Using DbContext factory 'DesignTimeMyDbContextFactory'.
Using context 'MyDbContext'.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'MyWebProject'...
No design-time services were found.
Migrating using database 'MyDb' on server 'MyDbServer.eu-west-1.rds.amazonaws.com'.
Opening connection to database 'MyDb' on server 'MyDbServer.eu-west-1.rds.amazonaws.com'.
An error occurred using the connection to database 'MyDb' on server 'MyDbServer.eu-west-1.rds.amazonaws.com'.
'MyDbContext' disposed.
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
... a lot of System.Data.SqlClient and Microsoft.EntityFrameworkCore stacktrace ...
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
ClientConnectionId:00000000-0000-0000-0000-000000000000
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
解决方案
推荐阅读
- mysql - (My)SQL - 是否可以在引用表中有唯一约束
- javascript - 我的图像无法加载.....“找不到图像”有什么想法吗?
- javascript - 如何在移动 Chrome 浏览器中的文本框的 focus() 事件上打开键盘
- azure - 带有 terraform 的 Azure Function 应用源代码控制
- php - PHP - 从表中选择随机行
- java - Minecraft Forge 修改不适用于非 OP 玩家的专用服务器
- python - 如何在 python / selenium 中正确处理 2-factor auth
- reactjs - 从支付网关重定向后出现 405 HTTP 错误
- c - 自适应网格细化
- node.js - 如何从所有分片中获取 users.cache 并将它们放入集合中,就像集合 client.user.cache 返回一样?