无法使用来自GitLab CI脚本的EF Core连接到AWS数据库

时间:2018-08-06 13:33:34

标签: entity-framework docker .net-core gitlab amazon-rds

我有一个使用Visual Studio 2017模板构建的AWS无服务器应用程序(.NET Core 2.0)。该应用程序使用Entity Framework Code-First来访问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)

0 个答案:

没有答案