title: Error on UWP trying to connect to SQL Server Express via EFCore
body: <p>Team,
  this is driving me crazy. I always receive this error when I start the UWP application:</p>
  
  <blockquote>
    <p>Microsoft.Data.SqlClient.SqlException: '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: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)'</p>
  </blockquote>
  
  <p>Inner Exception:</p>
  
  <blockquote>
    <p>Microsoft.Data.SqlClient.SqlException
      HResult=0x80131904
      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: Shared Memory Provider, error: 40 - Could not open a connection to SQL Server)
      Source=Core Microsoft SqlClient Data Provider
      StackTrace:
       at Microsoft.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, DbConnectionPool pool, SqlAuthenticationProviderManager sqlAuthProviderManager)
       at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
       at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
       at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal&amp; connection)
       at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions)
       at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions)
       at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource<code>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.QueryingEnumerable</code>1.Enumerator.InitializeReader(DbContext _, Boolean result)
       at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func<code>3 operation, Func</code>3 verifySucceeded)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable<code>1.Enumerator.MoveNext()
       at System.Collections.Generic.List</code>1.AddEnumerable(IEnumerable<code>1 enumerable)
       at System.Collections.Generic.List</code>1..ctor(IEnumerable<code>1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable</code>1 source)
       at UWP_EF_i9.MainPage.Page_Loaded(Object sender, RoutedEventArgs e) in E:\Users\Massimo\OneDrive\Development\Source\UWP EF i9\UWP EF i9\MainPage.xaml.cs:line 35</p>
  </blockquote>
  
  <p>Inner Exception 1:
  Win32Exception: Access is denied</p>
  
  <p>As EFCore in UWP is not able to dbscaffold the database, following the suggestions found here I did an EF.Core WPF application, scaffolded the database creating the classes... Imported the generated classes in the UWP application (renamed the namespace where applicable).</p>
  
  <p>In the UWP application I've added the Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SQLServer and added the Enterprise Capability: Enterprise Authentication</p>
  
  <p>So everything should be fine... but.. it does not work. Exactly the same LINQ query that works fine in the WPF gives me the error in the UWP</p>
  
  <p>Any idea? what am I missing?</p>
  
  <p>--- Edit 1</p>
  
  <p>I've even added the code from this page:</p>
  
  <p><a href="https://docs.microsoft.com/en-us/windows/uwp/data-access/sql-server-databases" rel="nofollow noreferrer">Use a SQL Server database in a UWP app</a>
  connectionString = "Data Source=YourServerName\SQLEXPRESS;Initial Catalog=NORTHWIND;Integrated Security=SSPI";</p>
  
  <p>so to my case:</p>
  
  <pre><code>            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())
                              {
                              }
                          }
                      }
                  }
              }
          }
  </code></pre>
  
  <p>but the conn.Open() fails with the same message</p>