我看到了很多类似的问题,但都没有解决我的问题。
我的设置如下:
- 带有数据库的远程主机:可以在
127.0.0.1:1433
上访问 - SSH隧道:
L5000 -> 127.0.0.1:1433
当我在SQL管理工具中输入服务器名称127.0.0.1,5000
时,我可以连接到想要的数据库。
如果我相应地调整我的连接字符串并启动应用程序,我会得到以下异常:
Type : SqlException
Message : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Source : Core .Net SqlClient Data Provider
HResult : -2146232060
TargetSite : .ctor
----- INNER EXCEPTION -----
Type : Win32Exception
Message : The system cannot find the file specified
HResult : -2147467259
我可以通过管理工具连接,所以我认为问题可能出在我的连接字符串或与Entity Framework Core相关的某些内容上。以下是我测试过的一些连接字符串:
Data Source=127.0.0.1,5000;Network Library=DBMSSOCN;Initial Catalog=Mux;Integrated Security=False;Persist Security Info=False;User ID=...;Password=...;MultipleActiveResultSets=True;Connect Timeout=5;Encrypt=True;TrustServerCertificate=True;Application Name=Mux;ConnectRetryCount=3;ConnectRetryInterval=5
Data Source=127.0.0.1;port=5000;Network Library=DBMSSOCN;Initial Catalog=Mux;Integrated Security=False;Persist Security Info=False;User ID=...;Password=...;MultipleActiveResultSets=True;Connect Timeout=5;Encrypt=True;TrustServerCertificate=True;Application Name=Mux;ConnectRetryCount=3;ConnectRetryInterval=5
Server=127.0.0.1;port=5000;Network Library=DBMSSOCN;Initial Catalog=Mux;Integrated Security=False;Persist Security Info=False;User ID=...;Password=...;MultipleActiveResultSets=True;Connect Timeout=5;Encrypt=True;TrustServerCertificate=True;Application Name=Mux;ConnectRetryCount=3;ConnectRetryInterval=5
Server=127.0.0.1,5000;Initial Catalog=Mux;Integrated Security=False;Persist Security Info=False;User ID=...;Password=...;MultipleActiveResultSets=True;Connect Timeout=5;Encrypt=True;TrustServerCertificate=True;Application Name=Mux;ConnectRetryCount=3;ConnectRetryInterval=5
Server=127.0.0.1;port=5000;Initial Catalog=Mux;Integrated Security=False;Persist Security Info=False;User ID=...;Password=...;MultipleActiveResultSets=True;Connect Timeout=5;Encrypt=True;TrustServerCertificate=True;Application Name=Mux;ConnectRetryCount=3;ConnectRetryInterval=5
Server=tcp,127.0.0.1,5000;Initial Catalog=Mux;Integrated Security=False;Persist Security Info=False;User ID=...;Password=...;MultipleActiveResultSets=True;Connect Timeout=5;Encrypt=True;TrustServerCertificate=True;Application Name=Mux;ConnectRetryCount=3;ConnectRetryInterval=5
有人知道这是为什么吗?
Edit 1 我进行了更多的测试;如果我尝试启动我的应用程序,则SQL Server Profiler不会注册任何活动。
此外,我尝试了这篇帖子中的方法..使用... tcp: ...
而不是... tcp, ...
..这也没有起作用。
我尝试在Visual Studio中添加数据源 - 测试连接:成功。我甚至复制了显示在那里的连接字符串,将其粘贴到我的应用程序中,启动它-但不起作用。为了说明这一点:是的,该应用程序确实使用提供的连接字符串。
Visual Studio和SQL Management Studio是否以与使用连接字符串不同的特殊方式连接到服务器?
Edit 2 根据您的评论,我想添加以下信息:
项目依赖项:
- Microsoft.EntityFrameworkCore.SqlServer v2.0.3
- Microsoft.EntityFrameworkCore.Tools v2.0.3
- Microsoft.VisualStudio.Web.CodeGeneration.Design v2.0.4
完整的日志消息:
Type : SqlException
Message : A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
Source : Core .Net SqlClient Data Provider
HResult : -2146232060
TargetSite : .ctor
Stacktrace : at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, 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.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, Func`2 verifySucceeded, TState state)
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at ch.wuerth.tobias.mux.Data.DataContextFactory.GetInstance()
at ch.wuerth.tobias.mux.plugins.PluginMusicBrainz.PluginMusicBrainz.Process(String[] args) in C:\Users\Tobias\Desktop\dev\mux-cli\plugins\PluginMusicBrainz\PluginMusicBrainz.cs:line 136
at ch.wuerth.tobias.mux.Core.plugin.PluginBase.Work(String[] args)
at ch.wuerth.tobias.mux.App.Program..ctor(String[] args) in C:\Users\Tobias\Desktop\dev\mux-cli\App\Program.cs:line 47
Data :
-> [HelpLink.ProdName, Microsoft SQL Server]
-> [HelpLink.EvtSrc, MSSQLServer]
-> [HelpLink.EvtID, 2]
-> [HelpLink.BaseHelpUrl, http://go.microsoft.com/fwlink]
-> [HelpLink.LinkId, 20476]
----- INNER EXCEPTION -----
Type : Win32Exception
Message : The system cannot find the file specified
HResult : -2147467259
(内部异常没有堆栈跟踪,因为它必须未定义)
我的DbContext像这样初始化连接字符串:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("..my connection string..");
}
编辑3
我很有理由相信问题一定与DbContext
的初始化有关,否则如何解释MSSQL Studio和应用程序的不同行为呢。
此外,对于那些寻求一些背景信息的人: 我正在开发这个应用程序。除了我的本地开发环境之外,我还有一个生产服务器,在那里我部署了我的主分支。在生产服务器上还有一个独立的数据库,其中包含生产数据,就像人们所期望的那样。我的问题是,我在生产服务器上进行数据操作(导入、处理等)的CLI会抛出异常,这是由数据引起的,而我无法在开发环境中再现这个错误。我想尝试从我的开发环境连接到生产数据库,而不是尝试复制整个数据库(它有几十亿字节大)。这将使我能够调试异常并修复问题。也许有人有其他的想法,可以让我通过实时数据库调试我的应用程序?