ASP.NET Core Web API Entity Framework Invalid object name only when running through IIS Server

0

I have a web api which uses entity framework to land information from a web form in to a SQL Server database table. When running the api locally I can post to localhost and the records are landed in to the SQL Server database table with no issues.

I have deployed the api to a test server which is using IIS to host the api. The account running the application pool as the same admin rights to the SQL Server database as my account which I am logged on to when running the API locally.

Here is the inner error message returned from the hosted api

Couldn't Save record. Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'dbo.fact_comment'.
   at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at Microsoft.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at Microsoft.Data.SqlClient.SqlDataReader.get_MetaData()
   at Microsoft.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   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)
   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)
   at Microsoft.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader()
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
ClientConnectionId:e3a221a7-a71b-44c7-b846-45a9e939ae77
Error Number:208,State:1,Class:16

Since I can post to the api when it is running locally I do not think there is anything wrong with the API. I confirmed the post is successfully inserting a new record in to the database table. So the API has all the correct connection information setup.

I do not know where to look for why the hosted API is returning an error being unable to find the table.

api
asp.net-core
iis
asked on Stack Overflow May 8, 2021 by Tom

1 Answer

0

I found the problem. I was accidently re-using the same name of an existing environmental variable for a different database on the same SQL Server.

Created a new environmental variable for the needed database.

answered on Stack Overflow May 8, 2021 by Tom

User contributions licensed under CC BY-SA 3.0