UWP应用通过EF Core连接SQL Server Express时遇连接错误求助
Hey Massimo, let's work through this frustrating connection issue together. The "access denied" inner exception is a key clue here—UWP's sandboxed environment adds unique constraints that might be tripping you up. Here are targeted steps to diagnose and fix the problem:
1. Verify UWP App Capabilities
UWP apps run in a strict sandbox, so you need to explicitly grant network access beyond just Enterprise Authentication:
- Open your
Package.appxmanifest(or use the Visual Studio Package Designer) - Navigate to the Capabilities tab
- Ensure these are checked:
- Private Networks (Client & Server): Required for accessing local/intranet SQL Server instances
- Enterprise Authentication: Double-check this is enabled (you mentioned it, but it's worth confirming)
- Save the manifest and rebuild your app
2. Fix Your Connection String
The shared memory provider error suggests UWP might struggle with that protocol. Try forcing TCP/IP in your connection string, and verify all parameters:
// Example connection string with TCP/IP forced string connectionString = @"Server=localhost,1433;Database=YourDatabaseName;Integrated Security=True;Network Library=DBMSSOCN";
- Replace
localhost,1433with your actual server address and port (default is 1433; check SQL Server Configuration Manager for your instance's port) - Ensure
Integrated Security=True(orTrusted_Connection=True) is present if using Windows Authentication - For named instances like
SQLEXPRESS, useServer=localhost\SQLEXPRESS,1433
3. Check SQL Server Permissions & Configuration
The "access denied" error points to permission mismatches between the UWP app's identity and SQL Server:
- Authentication Mode: Open SQL Server Management Studio (SSMS), right-click your server → Properties → Security → Ensure Windows Authentication mode is enabled
- User Login Permissions: In SSMS, go to Security → Logins and confirm your Windows user account has a login. Map this login to a user in your target database with appropriate permissions (e.g.,
db_datareader/db_datawriter) - Enable TCP/IP Protocol: Open SQL Server Configuration Manager → SQL Server Network Configuration → Protocols for [Your Instance] → Enable TCP/IP. Restart the SQL Server service after making this change.
4. Validate Direct Connection (Bypass EF Core)
You tried a raw SqlConnection already, but let's refine that test to isolate the issue:
try { string connString = (App.Current as App).ConnectionString; // Force TCP/IP if not already in the connection string if (!connString.Contains("Network Library")) { connString += ";Network Library=DBMSSOCN"; } using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); Debug.WriteLine("Connection successful!"); // Run a simple test query using (SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM tAPP", conn)) { int recordCount = (int)cmd.ExecuteScalar(); Debug.WriteLine($"Found {recordCount} records in tAPP"); } } } catch (Exception ex) { Debug.WriteLine($"Error: {ex.Message}\nInner Exception: {ex.InnerException?.Message}"); }
Run this and check the debug output—if it still fails, the problem is definitely at the network/permission level, not EF Core.
5. Ensure EF Core Version Compatibility
Make sure your UWP app's NuGet packages are compatible:
Microsoft.EntityFrameworkCoreandMicrosoft.EntityFrameworkCore.SqlServershould share the same major versionMicrosoft.Data.SqlClientmust be compatible with your EF Core version (check EF Core docs for supported versions)
6. Test Outside the UWP Sandbox
To rule out sandbox-specific issues, run the same connection logic in a console app targeting .NET Framework/.NET Core. If that works, the problem is strictly UWP-related (capabilities or sandbox identity restrictions).
内容的提问来源于stack exchange,提问作者Massimo Savazzi

