上下文
当前,我正在使用.NET Core创建用C#编写的提取,转换和加载(ETL)应用程序。 ETL应用程序的目标是通过Entity Framework Core 2.1.0和更高版本2.1.1访问的数据库。由于模式是固定的,因此使用数据库优先方法。 Microsoft提供了以下guide来解决这种情况。该指南指出,Package Manager控制台中的CLI命令Scaffold-DbContext
可用于生成DbContext和相应的实体模型。
在此项目的早期,我已经成功使用了此CLI命令。但是,模型的命名并不直接对应于表名和列名。此CLI工具的较新版本提供了一个选项-UseDatabaseNames
,因此我选择重新运行此命令以生成一些更新的模型。另外,我计划在将来重新生成DbContext。
问题
不幸的是,现在运行Scaffold-DbContext
命令将返回以下错误:Instance failure.
。详细选项使您可以更深入地了解出了什么问题,但是我不清楚到底出了什么问题以及如何解决。
命令
Scaffold-DbContext "[ConnectionString]" Microsoft.EntityFrameworkCore.SqlServer -UseDatabaseNames -OutputDir Models -v -force
详细输出
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.
异常
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.
如前所述,我不清楚到底出了什么问题。我的Google搜索没有任何相关信息。另外,我尝试过更新SDK,工具和其他参考。我尝试删除DbContext和模型。任何帮助将不胜感激!
答案 0 :(得分:3)
我认为您需要更改实例名称。
例如:
Scaffold-DbContext“服务器= PC \ SQLEXPRESS;数据库= ***;用户 ID = sa; Password = ****“ Microsoft.EntityFrameworkCore.SqlServer -OutputDir模型
因此,在这种情况下,您的实例为:PC\\SQLEXPRESS
。
该实例应如下所示:.\SQLEXPRESS
答案 1 :(得分:2)
是的,这通常是在PM控制台上的scaffold-dbcontext命令中错误地拼写了实例名称的问题。
您可以使用这个小的TSQL来找出您的SQL Server实例的确切拼写:SELECT @@SERVERNAME
答案 2 :(得分:1)
哈哈。是的,实例错误。我从一个类中复制了连接字符串。我的sql server实例是“ PC \ Expresss”,但是我的连接字符串是“ PC \ Express”,只是删除了反斜杠。