Scaffold-DbContext (EF Core Tools) throws 'Instance failure' exception
M

3

31

Context

Currently I am creating an Extract, Transform and Load (ETL) application written in C# with .NET Core. The target of the ETL application is a database that is accessed through Entity Framework Core 2.1.0 and later 2.1.1. A database first approach is used, since the schema's are fixed. Microsoft offers the following guide to approach this situation. The guide states that the CLI command Scaffold-DbContext in the Package Manager Console can be used to generate the DbContext and the corresponding entity models.

Earlier in this project I have successfully used this CLI command. However the naming of the models did not correspond directly to the table and column names. The newer versions of this CLI Tooling offers a option -UseDatabaseNames, so I therefore opted to rerun this command in order to generate some updated models. Additionally, regenerating the DbContext is something I am planning to do more often in the future.

The Problem

Unfortunately running the Scaffold-DbContext command now returns the following error: Instance failure.. The verbose option gives some more insight into what is going wrong, but I am unclear as to what exactly is going wrong and how to resolve it.

Command

Scaffold-DbContext "[ConnectionString]" Microsoft.EntityFrameworkCore.SqlServer -UseDatabaseNames -OutputDir Models -v -force

Verbose Output

Using project '[ProjectName]'.
Using startup project '[ProjectName]'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service\bin\Debug\netcoreapp2.1\[ProjectName].Service.deps.json --additionalprobingpath C:\Users\[MyComputerUsername]\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service\bin\Debug\netcoreapp2.1\[ProjectName].Service.runtimeconfig.json C:\Users\[MyComputerUsername]\.nuget\packages\microsoft.entityframeworkcore.tools\2.1.1\tools\netcoreapp2.0\any\ef.dll dbcontext scaffold Server=[ConnectionString] Microsoft.EntityFrameworkCore.SqlServer --json --output-dir Models --use-database-names --force --verbose --no-color --prefix-output --assembly C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service\bin\Debug\netcoreapp2.1\[ProjectName].Service.dll --startup-assembly C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service\bin\Debug\netcoreapp2.1\[ProjectName].Service.dll --project-dir C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service\ --language C# --working-dir C:\Repos\[Client]\[ProjectName]\[ProjectName] --root-namespace [ProjectName].Service
Using assembly '[ProjectName]'.
Using startup assembly '[ProjectName]'.
Using application base 'C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service\bin\Debug\netcoreapp2.1'.
Using working directory 'C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service'.
Using root namespace '[ProjectName]'.
Using project directory 'C:\Repos\[Client]\[ProjectName]\[ProjectName]\[ProjectName].Service\'.
The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.1.2-rtm-30932'. Update the tools for the latest features and bug fixes.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding design-time services referenced by assembly '[ProjectName]'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly '[ProjectName]'...
No design-time services were found.

Exception

System.InvalidOperationException: Instance failure.
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
   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)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(DbConnection connection, IEnumerable`1 tables, IEnumerable`1 schemas)
   at Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal.SqlServerDatabaseModelFactory.Create(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas)
   at Microsoft.EntityFrameworkCore.Scaffolding.Internal.ReverseEngineerScaffolder.ScaffoldModel(String connectionString, IEnumerable`1 tables, IEnumerable`1 schemas, String namespace, String language, String contextDir, String contextName, ModelReverseEngineerOptions modelOptions, ModelCodeGenerationOptions codeOptions)
   at Microsoft.EntityFrameworkCore.Design.Internal.DatabaseOperations.ScaffoldContext(String provider, String connectionString, String outputDir, String outputContextDir, String dbContextClassName, IEnumerable`1 schemas, IEnumerable`1 tables, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContextImpl(String provider, String connectionString, String outputDir, String outputDbContextDir, String dbContextClassName, IEnumerable`1 schemaFilters, IEnumerable`1 tableFilters, Boolean useDataAnnotations, Boolean overwriteFiles, Boolean useDatabaseNames)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScaffoldContext.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Instance failure.

As stated before I am unclear what exactly is going wrong. My google searches have not resulted in any relevant information. Additionally I have tried updating the SDK, tools and other references. And I tried deleting the DbContext and models. Any help would be greatly appreciated!

Modulation answered 5/9, 2018 at 11:23 Comment(4)
Is the database instance in the connection string correct?Exciter
Thank you so much! Apparently the connection string structure has an effect. The structure of Server=[Name1]\\[Name2];Database=[Databasename];Trusted_Connection=True; (both with and without the double backslash) resulted in an exception. The connection string Data Source=[Name1]\[Name2];Initial Catalog=[DatabaseName];Integrated Security=True; did work however. If you would formulate this comment as an answer, I will accept and upvote it.Modulation
I'd love to, but I don't really understand why the "server" keyword is refused. According to the docs, "server" and "data source" are equivalent (see "Data Source -or- Server -or- Address -or- Addr -or- Network Address").Exciter
In my case I deleted obj and bin folder and it worked.Ejecta
A
73

I think you need to change your instance name.

For example :

Scaffold-DbContext "Server=PC\SQLEXPRESS;Database=***;User ID=sa;Password=****" Microsoft.EntityFrameworkCore.SqlServer -OutputDir model

So your instance in this case is : PC\\SQLEXPRESS. This instance should be like this : .\SQLEXPRESS

Anglian answered 17/10, 2018 at 21:29 Comment(1)
You can still use the computer name, e.g. PC\SQLEXPRESS. It's just the double back slash that breaks it. Fixed it fo me ... voila!Alrick
O
47

This is indeed an instance error. I copied the connection string from one of my classes. My SQL server instance is PC\Expresss, but my connection string was PC\\Express, just removed a back slash.

Oleaceous answered 17/1, 2019 at 23:13 Comment(2)
Why?? It wasted my 2+ hours... Thanks for the solution!Genesis
Removing the extra back slash worked for me.Knuth
M
5

Yea this is usually a matter of mis-spelling the instance name in your scaffold-dbcontext command on the PM Console..

You can use this little TSQL to find out the exact spelling of your SQL Server instance: SELECT @@SERVERNAME

Markup answered 16/3, 2019 at 23:48 Comment(1)
Ok, sure, I just didn't get it was talking about that instance and not one EF was trying to create!! arg.Slaby

© 2022 - 2024 — McMap. All rights reserved.