title: Why do I get a "ConnectionString property has not been initialized" error on the first call to a stored procedure with Entity Framework Core?
body: <p>I have a service in ASP.NET Core to which I inject my Entity Framework Core context that is used to define a private func that my service will need.
  That func is about calling a stored procedure.</p>
  
  <p>The first call to that func systematically leads to an error </p>
  
  <blockquote>
    <p>The ConnectionString property has not been initialized.</p>
  </blockquote>
  
  <p>However, subsequent calls are fine.</p>
  
  <p>Here is the service class constructor:</p>
  
  <pre><code>private readonly Func&lt;Task&lt;long&gt;&gt; _myFunc;
  
  public MyService(MyContext context) =&gt; _myFunc= async () =&gt;
      {
          using (var connection = context.Database.GetDbConnection())
          {
              await connection.OpenAsync();
  
              using (var command = connection.CreateCommand())
              {
                  command.CommandType = CommandType.StoredProcedure;
                  command.CommandText = "sp_MyStoredProcedure";
                  command.Parameters.Add(new SqlParameter("@AParameter", SqlDbType.Int) { Value = 1 });
                  return (long)await command.ExecuteScalarAsync();
              }
          }
      };
  </code></pre>
  
  <p>Here is the <code>stacktrace</code>:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: The ConnectionString property has not been initialized.</p>
    
    <p>at System.Data.SqlClient.SqlConnection.PermissionDemand()<br>
    at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)<br>
    at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions)<br>
    at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions)<br>
    at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
           at System.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken)
        --- End of stack trace from previous location where exception was thrown ---
           at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)</p>
  </blockquote>
  
  <p>UPDATE:</p>
  
  <p>When I set a breakpoint on line <code>await connection.OpenAsync()</code> and look at the variable <code>connection</code> the connection string is there and correct.</p>
  
  <p>Note that <code>MyServices</code> is injected into a controller constructor.
  The service registration is done this way in <code>startup.cs</code>:</p>
  
  <pre><code>services.AddTransient&lt;IMyService, MyService&gt;();
  </code></pre>