title: Entity Framework Timeouts
body: <p>I am getting timeouts using the Entity Framework (EF) when using a function import that takes over 30 seconds to complete. I tried the following and have not been able to resolve this issue:</p>
  
  <p>I added <code>Default Command Timeout=300000</code> to the connection string in the <em>App.Config</em> file in the project that has the EDMX file as suggested <a href="https://stackoverflow.com/questions/1718739/entity-framework-with-mysql-timeout-expired-while-generating-model">here</a>.</p>
  
  <p>This is what my connection string looks like:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;add 
      name="MyEntityConnectionString" 
      connectionString="metadata=res://*/MyEntities.csdl|res://*/MyEntities.ssdl|
         res://*/MyEntities.msl;
         provider=System.Data.SqlClient;provider connection string=&amp;quot;
         Data Source=trekdevbox;Initial Catalog=StarTrekDatabase;
         Persist Security Info=True;User ID=JamesTKirk;Password=IsFriendsWithSpock;
         MultipleActiveResultSets=True;Default Command Timeout=300000;&amp;quot;"
      providerName="System.Data.EntityClient" /&gt;
  </code></pre>
  
  <p>I tried setting the CommandTimeout in my repository directly like so:</p>
  
  <pre><code>private TrekEntities context = new TrekEntities();
  
  public IEnumerable&lt;TrekMatches&gt; GetKirksFriends()
  {
      this.context.CommandTimeout = 180;
      return this.context.GetKirksFriends();
  }
  </code></pre>
  
  <p>What else can I do to get the EF from timing out? This only happens for very large datasets. Everything works fine with small datasets.</p>
  
  <p>Here is one of the errors I'm getting:</p>
  
  <blockquote>
    <p>System.Data.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.</p>
  </blockquote>
  
  <hr>
  
  <p>OK - I got this working and it's silly what happened. I had both the connection string with <code>Default Command Timeout=300000</code> and the CommandTimeout set to 180. When I removed the <code>Default Command Timeout</code> from the connection string, it worked. So the answer is to manually set the CommandTimeout in your repository on your context object like so:</p>
  
  <pre><code>this.context.CommandTimeout = 180;
  </code></pre>
  
  <p>Apparently setting the timeout settings in the connection string has no effect on it.</p>