首页 > 解决方案 > UWP 尝试通过 EFCore 连接到 SQL Server Express 时出错

问题描述

团队,这让我发疯。当我启动 UWP 应用程序时,我总是收到此错误:

Microsoft.Data.SqlClient.SqlException:'建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:共享内存提供者,错误:40 - 无法打开与 SQL Server 的连接)'

内部异常:

Microsoft.Data.SqlClient.SqlException HResult=0x80131904 消息=建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:共享内存提供者,错误:40 - 无法打开与 SQL Server 的连接)源 = 核心 Microsoft SqlClient 数据提供者 StackTrace:在 Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 标识,SqlConnectionString 连接选项,SqlCredential 凭据,对象 providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData,1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 次重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& connection) 在 Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 重试,DbConnectionOptions userOptions) 在 Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 retry) at Microsoft.Data.SqlClient.SqlConnection.Open() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1。 Enumerator.InitializeReader(DbContext _, Boolean result) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func 3 operation, Func3 verifySucceeded) 在 Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable 1.Enumerator.MoveNext() at System.Collections.Generic.List1.AddEnumerable (IEnumerable 1 enumerable) at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 来源)在 E:\Users\Massimo\OneDrive\Development\Source\UWP EF i9\UWP EF i9\MainPage.xaml.cs:line 35 中的 UWP_EF_i9.MainPage.Page_Loaded(Object sender, RoutedEventArgs e)

内部异常 1:Win32Exception:访问被拒绝

由于 UWP 中的 EFCore 无法对数据库进行 dbscaffold,因此按照此处找到的建议,我做了一个 EF.Core WPF 应用程序,为创建类的数据库搭建了脚手架......在 UWP 应用程序中导入了生成的类(在适用的情况下重命名了命名空间) .

在 UWP 应用程序中,我添加了 Microsoft.EntityFrameworkCore 和 Microsoft.EntityFrameworkCore.SQLServer 并添加了 Enterprise Capability: Enterprise Authentication

所以一切都应该没问题......但是......它不起作用。在 WPF 中工作正常的完全相同的 LINQ 查询给了我 UWP 中的错误

任何想法?我错过了什么?

--- 编辑 1

我什至从这个页面添加了代码:

在 UWP 应用中使用 SQL Server 数据库 connectionString = "Data Source=YourServerName\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=SSPI";

所以对我来说:

            try
        {
            using (SqlConnection conn = new SqlConnection((App.Current as App).ConnectionString))
            {
                conn.Open();
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "Select * from tAPP";
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                            }
                        }
                    }
                }
            }
        }

但是 conn.Open() 失败并显示相同的消息

标签: sql-serveruwpentity-framework-coredatabase-connectionef-core-3.0

解决方案


我发现了问题:(这里解释了: 在 UWP 应用程序中使用 SQL Server 数据库

在以下部分:连接到您的数据库时遇到问题?

TCP/IP 协议默认禁用,必须启用。现在 WPF 中的同一个应用程序怎么可能工作,VS2019 能够连接(更不用说 SSMS ......)并且只有 UWP 不连接对我来说是一个谜......但现在它工作了!


推荐阅读