SQL Server 在EF核心搭建时遇到SQL连接字符串错误?

atmip9wb  于 2022-12-22  发布在  其他
关注(0)|答案(3)|浏览(264)

For background info, I'm running .NET core on Arch Linux VScode and I also have SQL Server running on Arch Linux. When I connect to the server via sqlcmd or through the VScode SQL extension, everything works fine.
Only when scaffolding am I getting errors, so my connection string is probably wrong, and I've searched and basically tried everything within my grasp. Any help would be much appreciated, thanks in advance.
When I run the command

[wasiim@wasiim-PC WebApiServerApp]$ dotnet ef dbcontext scaffold "Server=wasiim-PC;Database=ConkerDb;User Id=sa;Password=********" Microsoft.EntityFrameworkCore.SqlServer -o Models -c ConkerDbContext -v

I get this error:
System.Data.SqlClient.SqlException (0x80131904): 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: TCP Provider, error: 35 - An internal exception was caught)
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 6): No such device or address
at System.Net.Dns.InternalGetHostByName(String hostName)
at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at System.Data.SqlClient.SNI.SNITCPHandle.Connect(String serverName, Int32 port, TimeSpan timeout)
at System.Data.SqlClient.SNI.SNITCPHandle..ctor(String serverName, Int32 port, Int64 timerExpire, Object callbackObject, Boolean parallel)
I've also tried doing

[wasiim@wasiim-PC WebApiServerApp]$ dotnet ef dbcontext scaffold "Server=(wasiim-PC)\MSSQLSERVER;Database=ConkerDb;User Id=sa;Password=********" Microsoft.EntityFrameworkCore.SqlServer -o Models -c ConkerDbContext -v

and I get the error
System.Data.SqlClient.SqlException (0x80131904): 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: SQL Network Interfaces, error: 25 - Connection string is not valid)
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, String accessToken)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
mssql error log
the errorlog display nothing about any of the connection attempts I've made so I'm assuming it didn't make a connection, and yes the service is running. I pinged wasiim-PC, pinged localhost, pinged 127.0.0.1 they're all working just fine.

[wasiim@wasiim-PC log]$ sudo cat errorlog
2019-07-04 14:53:24.34 Server      Microsoft SQL Server 2017 (RTM-CU15) (KB4498951) - 14.0.3162.1 (X64) 
        May 15 2019 19:14:30 
        Copyright (C) 2017 Microsoft Corporation
        Developer Edition (64-bit) on Linux (Arch Linux)
2019-07-04 14:53:24.35 Server      UTC adjustment: -5:00
2019-07-04 14:53:24.35 Server      (c) Microsoft Corporation.
2019-07-04 14:53:24.36 Server      All rights reserved.
2019-07-04 14:53:24.36 Server      Server process ID is 32.
2019-07-04 14:53:24.36 Server      Logging SQL Server messages in file '/var/opt/mssql/log/errorlog'.
2019-07-04 14:53:24.37 Server      Registry startup parameters: 
         -d /var/opt/mssql/data/master.mdf
         -l /var/opt/mssql/data/mastlog.ldf
         -e /var/opt/mssql/log/errorlog
2019-07-04 14:53:24.38 Server      SQL Server detected 1 sockets with 4 cores per socket and 8 logical processors per socket, 8 total logical processors; using 8 logical processors based on SQL Server licensing. This is an informational message; no user action is required.
2019-07-04 14:53:24.39 Server      SQL Server is starting at normal priority base (=7). This is an informational message only. No user action is required.
2019-07-04 14:53:24.39 Server      Detected 12632 MB of RAM. This is an informational message; no user action is required.
2019-07-04 14:53:24.40 Server      Using conventional memory in the memory manager.
2019-07-04 14:53:24.41 Server      Large Page Allocated: 32MB 
2019-07-04 14:53:24.92 Server      Buffer pool extension is already disabled. No action is necessary. 
2019-07-04 14:53:25.16 Server      InitializeExternalUserGroupSid failed. Implied authentication will be disabled.
2019-07-04 14:53:25.16 Server      Implied authentication manager initialization failed. Implied authentication will be disabled.
2019-07-04 14:53:25.18 Server      Successfully initialized the TLS configuration. Allowed TLS protocol versions are ['1.0 1.1 1.2']. Allowed TLS ciphers are ['ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:!DHE-RSA-AES256-GCM-SHA384:!DHE-RSA-AES128-GCM-SHA256:!DHE-RSA-AES256-SHA:!DHE-RSA-AES128-SHA'].
2019-07-04 14:53:25.21 Server      The maximum number of dedicated administrator connections for this instance is '1'
2019-07-04 14:53:25.22 Server      Node configuration: node 0: CPU mask: 0x00000000000000ff:0 Active CPU mask: 0x00000000000000ff:0. This message provides a description of the NUMA configuration for this computer. This is an informational message only. No user action is required.
2019-07-04 14:53:25.22 Server      Using dynamic lock allocation.  Initial allocation of 2500 Lock blocks and 5000 Lock Owner blocks per node.  This is an informational message only.  No user action is required.
2019-07-04 14:53:25.23 Server      In-Memory OLTP initialized on standard machine.
2019-07-04 14:53:25.34 Server      Database Instant File Initialization: enabled. For security and performance considerations see the topic 'Database Instant File Initialization' in SQL Server Books Online. This is an informational message only. No user action is required.
2019-07-04 14:53:25.35 Server      Query Store settings initialized with enabled = 1, 
2019-07-04 14:53:25.35 spid6s      Starting up database 'master'.
2019-07-04 14:53:25.35 Server      Software Usage Metrics is disabled.
2019-07-04 14:53:25.57 spid6s      Resource governor reconfiguration succeeded.
2019-07-04 14:53:25.57 spid6s      SQL Server Audit is starting the audits. This is an informational message. No user action is required.
2019-07-04 14:53:25.58 spid6s      SQL Server Audit has started the audits. This is an informational message. No user action is required.
2019-07-04 14:53:25.67 spid6s      SQL Trace ID 1 was started by login "sa".
2019-07-04 14:53:25.69 spid6s      Server name is 'wasiim-PC'. This is an informational message only. No user action is required.
2019-07-04 14:53:25.72 spid24s     Always On: The availability replica manager is starting. This is an informational message only. No user action is required.
2019-07-04 14:53:25.72 spid22s     Starting up database 'msdb'.
2019-07-04 14:53:25.72 spid23s     Starting up database 'ConkerDb'.
2019-07-04 14:53:25.72 spid9s      Starting up database 'mssqlsystemresource'.
2019-07-04 14:53:25.73 spid24s     Always On: The availability replica manager is waiting for the instance of SQL Server to allow client connections. This is an informational message only. No user action is required.
2019-07-04 14:53:25.74 spid9s      The resource database build version is 14.00.3162. This is an informational message only. No user action is required.
2019-07-04 14:53:25.78 spid9s      Starting up database 'model'.
2019-07-04 14:53:26.02 spid23s     Parallel redo is started for database 'ConkerDb' with worker pool size [4].
2019-07-04 14:53:26.02 spid19s     A self-generated certificate was successfully loaded for encryption.
2019-07-04 14:53:26.03 spid19s     Server is listening on [ 'any' <ipv6> 1433].
2019-07-04 14:53:26.04 spid19s     Server is listening on [ 'any' <ipv4> 1433].
2019-07-04 14:53:26.04 Server      Server is listening on [ ::1 <ipv6> 1434].
2019-07-04 14:53:26.04 Server      Server is listening on [ 127.0.0.1 <ipv4> 1434].
2019-07-04 14:53:26.04 Server      Dedicated admin connection support was established for listening locally on port 1434.
2019-07-04 14:53:26.05 spid19s     SQL Server is now ready for client connections. This is an informational message; no user action is required.
2019-07-04 14:53:26.08 spid9s      Polybase feature disabled.
2019-07-04 14:53:26.09 spid9s      Clearing tempdb database.
2019-07-04 14:53:26.13 spid6s      Parallel redo is shutdown for database 'ConkerDb' with worker pool size [4].
2019-07-04 14:53:26.15 Server      Failed to verify the Authenticode signature of 'C:\binn\secforwarder.dll'. Signature verification of SQL Server DLLs will be skipped. Genuine copies of SQL Server are signed. Failure to verify the Authenticode signature might indicate that this is not an authentic release of SQL Server. Install a genuine copy of SQL Server or contact customer support.
2019-07-04 14:53:26.38 spid9s      Starting up database 'tempdb'.
2019-07-04 14:53:26.63 spid9s      The tempdb database has 1 data file(s).
2019-07-04 14:53:26.64 spid24s     The Service Broker endpoint is in disabled or stopped state.
2019-07-04 14:53:26.65 spid24s     The Database Mirroring endpoint is in disabled or stopped state.
2019-07-04 14:53:26.67 spid24s     Service Broker manager has started.
2019-07-04 14:53:26.70 spid6s      Recovery is complete. This is an informational message only. No user action is required.
2019-07-04 14:58:44.68 spid51      Using 'dbghelp.dll' version '4.0.5'
2019-07-04 15:00:33.43 spid51      Attempting to load library 'xplog70.dll' into memory. This is an informational message only. No user action is required.
2019-07-04 15:00:33.54 spid51      Using 'xplog70.dll' version '2017.140.3162' to execute extended stored procedure 'xp_msver'. This is an informational message only; no user action is required.

Update: full error
Using project '/home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/WebApiServerApp.csproj'. Using startup project '/home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/WebApiServerApp.csproj'. Writing '/home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/obj/WebApiServerApp.csproj.EntityFrameworkCore.targets'... dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpDCtI3l.tmp /verbosity:quiet /nologo /home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/WebApiServerApp.csproj Writing '/home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/obj/WebApiServerApp.csproj.EntityFrameworkCore.targets'... dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpPGLuFn.tmp /verbosity:quiet /nologo /home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/WebApiServerApp.csproj dotnet build /home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/WebApiServerApp.csproj /p:GenerateRuntimeConfigurationFiles=True /verbosity:quiet /nologo
Build succeeded. 0 Warning(s) 0 Error(s)

已用时间00:00:03.18 dotnet exec --depsfile/home/wasiim/文档/编码项目/项目连接器/WebApiServerApp/bin/调试/netcoreapp2.2/WebApiServerApp. deps. json--附加探测路径/home/wasiim/.nuget/包--运行时间配置/home/wasiim/文档/编码项目/项目连接器/WebApiServerApp/bin/调试/netcoreapp2.2/WebApiServerApp.运行时间配置. json/home/wasiim/. nuget/包/微软实体框架核心.工具. dotnet/2.0.3/工具/netcoreapp2.0/ef. dll数据库上下文脚手架“服务器=(wasiim-PC)\MSSQLSERVER;数据库=ConkerDb;用户Id=sa;密码= come 1517”Microsoft. EntityFramework核心.SqlServer -o模型-c ConkerDbContext --程序集/主目录/wasim/文档/编码项目/项目Conker/WebApiServerApp/bin/调试/netcoreapp2.2/WebApiServerApp. dll--启动程序集/主目录/wasim/文档/编码项目/项目Conker/WebApiServerApp/bin/调试/netcoreapp2.2/WebApiServerApp. dll--项目目录/主目录/wasim/文档/编码项目/项目Conker/WebApiServerApp/ --详细--根目录-命名空间WebApiServerApp使用程序集“WebApiServerApp”。使用启动程序集“WebApiServerApp”。使用应用程序基“/home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/bin/Debug/netcoreapp2.2”。使用工作目录“/home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp "。使用根命名空间”WebApiServerApp“。使用项目目录”/home/wasiim/Documents/CodingProjects/ProjectConker/WebApiServerApp/“。正在查找提供程序“Microsoft.EntityFrameworkCore. SqlServer”的设计时服务...正在使用提供程序“Microsoft.EntityFrameworkCore. SqlServer”中的设计时服务。正在查找程序集“WebApiServerApp”引用的设计时服务。未找到引用的设计时服务。正在程序集“WebApiServerApp”中查找IDesignTimeServices实现...未找到设计时服务。
正如你所看到的,在我得到异常之前,它说没有设计时服务被发现,这似乎是相关的,我在网上不知疲倦地搜索,但不能修复这个问题。希望有人能帮助我,提前感谢。

11dmarpk

11dmarpk1#

这就是我一直在说的。我不认为网络错误信息是准确的。网络正在尝试使用IP地址进行安全的TCP连接(名称)和端口号。连接未完成,网络提示错误可能是由于服务引起的。TCP错误失败有四个原因:1)没有到服务器的路由2)端口被阻止3)服务器未侦听4)TLS/安全连接的SSL失败。
从日志文件(LDF)中,我们可以知道SQL Server从未获得连接,因为没有登录记录。我认为是时候使用嗅探器,如fiddler的wireshark,以查看连接失败的确切位置。
我不知道如何配置linux,怀疑设置错误。我不确定linux和windows使用相同的IP地址是否有效。发送消息是好的。但是当接收到一个IP而linux和Windows没有不同的IP地址时,以太网接口不知道是在Windows接口还是Linux接口上发送和接收消息。看起来在这种情况下将使用端口号。端口1433为Linux和端口1434为Windows。使用嗅探器将确认。所以我将首先通过使用SSMS获得网络端的工作,并获得SSMS的工作,这应该解决网络问题。

jslywgbw

jslywgbw2#

我运行了这个,我想这次它成功了:

dotnet ef dbcontext scaffold "Server=wasiim-PC;Database=ConkerDb;User Id=sa;Password=*********" Microsoft.EntityFrameworkCore.SqlServer -o Models -c ConkerDbContext -v
ppcbkaq5

ppcbkaq53#

一开始我的连接字符串如下:-

dotnet ef dbcontext scaffold "Server=(localdb)\\mssqllocaldb;Database=DB_NAME;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -o Models

以连接字符串中的两个\\为例,这就是问题所在。
然后我把它做成这样,它对我很有效。

dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=DB_NAME;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -o Models

相关问题