I have an AWS serverless application (.NET Core 2.0) built using the Visual Studio 2017 template. The application accesses a MSSQL database hosted in AWS RDS, using Entity Framework Code-First.
The source code is hosted at GitLab.com, and I have a CI script to built, test and deploy the application. The script is run using a docker container supplied by GitLab.com. As part of the deployment step I want to update the database with any new migrations. To do that I use the dotnet ef database update
command.
The problem:
In the CI script the database update command fails, saying that the server was not found or was not accessible. However, if I run the same command from my local machine the command succeeds.
The script outputs the dotnet version and the dotnet ef version, and they are exactly the same as on my local machine. I have also added verbose output for the command to verify that it is in fact trying to connect to the correct database.
Any suggestions to what the cause of the problem is, and how I can fix it?
Here is my CI script:
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
Here are the dotnet versions involved - as mentioned I get the same versions on my local machine:
$ dotnet --version
2.1.302
$ dotnet ef --version
Entity Framework Core .NET Command-line Tools
2.1.1-rtm-30846
Here is the most relevant parts from the CI output:
$ 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)
User contributions licensed under CC BY-SA 3.0