0

I'm developing a complex solution where I use Entity Framework 6 for my data layer operations. So far everything works as expected. But at startup of my application I get a strange exception, which doesn't affect the startup, but I'm curious, why its thrown.

The application has three different contexts: data, results, web. Each of them is using an according schema name. But I do not use the default schema 'dbo' for anything. Yet, for some reason the application is doing stuff with this default schema 'dbo' aynways and I dont know why. This only happens while the application is starting, but my dependency injection container should be already configured to habe all needed contexts, which are referencing the correct schema names and not 'dbo'.

The full stack trace of the exception is:

System.Data.SqlClient.SqlException
  HResult=0x80131904
  Message=Invalid object name 'dbo.__MigrationHistory'.
  Source=.Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption, Boolean shouldCacheForAlwaysEncrypted)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)

  This exception was originally thrown at this call stack:
    System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException, bool, System.Action<System.Action>)
    System.Data.SqlClient.SqlInternalConnection.OnError(System.Data.SqlClient.SqlException, bool, System.Action<System.Action>)
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(System.Data.SqlClient.TdsParserStateObject, bool, bool)
    System.Data.SqlClient.TdsParser.TryRun(System.Data.SqlClient.RunBehavior, System.Data.SqlClient.SqlCommand, System.Data.SqlClient.SqlDataReader, System.Data.SqlClient.BulkCopySimpleResultSet, System.Data.SqlClient.TdsParserStateObject, out bool)
    System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
    System.Data.SqlClient.SqlDataReader.MetaData.get()
    System.Data.SqlClient.SqlCommand.FinishExecuteReader(System.Data.SqlClient.SqlDataReader, System.Data.SqlClient.RunBehavior, string, bool, bool, bool)
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(System.Data.CommandBehavior, System.Data.SqlClient.RunBehavior, bool, bool, int, out System.Threading.Tasks.Task, bool, bool, System.Data.SqlClient.SqlDataReader, bool)

Has anyone ideas?

I already tried configuring entity framework by DbConfiguration class and did various changes to my three contexts. Also I tried a custom History Context, but I wanna keep my MigrationHistory tybles per custom schema name and dont want any of them to be in the dbo schema just so that the exception goes away.

Best regards,

Merlin

  • Does this answer your question? [Setting schema name for DbContext](https://stackoverflow.com/questions/30512218/setting-schema-name-for-dbcontext) – Max Naumov Jul 28 '23 at 10:46
  • Hi, thanks for the link, but no it does not. As I mentioned my contexts are working with my custom schemas. The exception occurs on startup once and then never again. – Merlin May Jul 28 '23 at 10:59
  • When you first run application all schemes were setup on the dbcontexts? Or it used the default dbo schema (in case if you haven't specified the schema initially) when entity Framework was creating migrations? – Max Naumov Jul 28 '23 at 11:02
  • The first thing my application does on startup is initializing the contexts and database connections. The exception is thrown when my frontend starts and executes the display of my main view – Merlin May Jul 28 '23 at 11:10
  • Try suggestions from here it's ef core but I think this should work for EF6 as well https://stackoverflow.com/questions/46205602/ef-core-migrations-with-multiple-db-schemas – Max Naumov Jul 28 '23 at 11:18
  • Have you tried [changing the schema for the Migrations History Table](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/history-table)? – sellotape Jul 28 '23 at 15:47

0 Answers0