首页 > 解决方案 > 无法使用 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)

标签: entity-frameworkdocker.net-coregitlabamazon-rds

解决方案


推荐阅读