代码之家  ›  专栏  ›  技术社区  ›  Thomas Jørgensen

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

  •  0
  • Thomas Jørgensen  · 技术社区  · 6 年前

    我有一个使用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版本,它们与本地计算机上的完全相同。我还为命令添加了详细输出,以验证它实际上是在尝试连接到正确的数据库。

    对问题的原因有何建议,以及如何解决?

    这是我的词脚本:

    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 回复  |  直到 6 年前