Search code examples
c#sql-server.net-coreamazon-rds

Connecting to AWS RDS from dotnetcore web app


I had set up a .NET Core project, which would use a database connection from an AWS RDS SQL Server instance and can't connect to it from the app. The strange is that from SSMS, Visual studio Server explorer I can see the database, only the app refuses to connect.

Base setup:

  • RDS SQL Server instance is up and running, the db is contained, uses forceSSL
  • inbound/outbound rules set to allow SQL Server type traffic

inbound/outbound rules

  • database filled with data for testing

  • using this connection string:

    "DefaultConnection": "Server=erp-devtest...amazonaws.com;Initial Catalog=...;Persist Security Info=True;User ID=...;Password=..."

Connecting to the database from:

  • SSMS = ok
  • Visual Studio Server Explorer = ok
  • .NET Core app = nok

Seeing the logs it seems that the app can't reach the server as nothing on the server side is triggered.

The app gets this error msg when it tries to connect:

"   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()\r\n   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean isAsync, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n   at System.Data.Common.DbCommand.ExecuteReader()\r\n   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)\r\n   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()\r\n   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)\r\n   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)\r\n   at DatabaseLayer.Core.Management.ProjectManagement.Get(SearchParameterDTO`1 searchParameterDTO) in ..."

Do you have any ideas how to solve this?

UPDATE

The call causing the error:

try
            {
                Invoice invoice = _contentDbContext.Invoice.FirstOrDefault();  
            }
catch (Exception ex)
            { }

where Invoice is an existing Table and class normally reached using EF Core.

ex is returning back with this error message:

ex = {"Invalid object name 'Invoice'."}

the exception has one error

[0] = {Microsoft.Data.SqlClient.SqlError: Invalid object name 'Invoice'.}

and the above mentioned stack trace is thrown by

ex.SerializationStackTraceString

When I use a working connection string pointing to an other DB, the Invoice is returned back as expected


Solution

  • Nevermind, everything was set correctly. What I messed up, I've restored the database from a slightly older backup, where my classes had the same names with ...set. So a single migration to the latest version did the trick.