gpt4 book ai didi

.net-core - Scaffold-DbContext(EF Core 工具)抛出 'Instance failure' 异常

转载 作者:行者123 更新时间:2023-12-02 22:56:12 28 4
gpt4 key购买 nike

上下文

目前,我正在使用 .NET Core 创建一个用 C# 编写的提取、转换和加载 (ETL) 应用程序。 ETL 应用程序的目标是通过 Entity Framework Core 2.1.0 和更高版本 2.1.1 访问的数据库。由于模式是固定的,因此使用数据库优先方法。 Microsoft 提供以下 guide来处理这种情况。该指南指出,包管理器控制台中的 CLI 命令 Scaffold-DbContext 可用于生成 DbContext 和相应的实体模型。

在此项目的早期,我已成功使用此 CLI 命令。然而,模型的命名并不直接对应于表和列的名称。此 CLI 工具的较新版本提供了选项 -UseDatabaseNames,因此我选择重新运行此命令以生成一些更新的模型。此外,我计划将来更频繁地重新生成 DbContext。

问题

不幸的是,运行Scaffold-DbContext命令现在会返回以下错误:实例失败。。详细选项可以更深入地了解出现的问题,但我不清楚到底出了什么问题以及如何解决它。

命令

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.

如前所述,我不清楚到底出了什么问题。我的谷歌搜索没有得到任何相关信息。此外,我还尝试更新 SDK、工具和其他引用资料。我尝试删除 DbContext 和模型。任何帮助将不胜感激!

最佳答案

我认为您需要更改实例名称。

例如:

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

因此,本例中您的实例是:PC\\SQLEXPRESS。该实例应如下所示:.\SQLEXPRESS

关于.net-core - Scaffold-DbContext(EF Core 工具)抛出 'Instance failure' 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52183959/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com