title: Entity Framework Timeout when called from WCF
body: <p>I have a .NET project that consists of the following:</p>
  
  <ol>
  <li>WCF web service</li>
  <li>Entity Framework data layer</li>
  </ol>
  
  <p>The ultimate issue described below is with the User table. The User table the schema is as follows:</p>
  
  <pre><code>CREATE TABLE [dbo].[User](
      [Id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
      [FirstName] [varchar](50) NOT NULL,
      [LastName] [varchar](50) NOT NULL,
      [Email] [varchar](50) NOT NULL,
      [Username] [varchar](50) NOT NULL,
      [Password] [varchar](250) NOT NULL,
      [AuthenticationKey] [uniqueidentifier] NULL,
      [Active] [bit] NOT NULL DEFAULT (1),
      [Admin] [bit] NOT NULL DEFAULT (0),
      [AutoSubmit] [bit] NOT NULL DEFAULT(0))
  
  CREATE UNIQUE NONCLUSTERED INDEX [IX_User] ON [dbo].[User] 
  (
          [Username] ASC
  )
  </code></pre>
  
  <p>If I run a query against the User table with 3 records in it, it executes in less than a second.</p>
  
  <p>I am running the following query against Entity Framework in a method called User.Validate().</p>
  
  <pre><code>database.User.Where(user =&gt; user.Username == userName &amp;&amp; user.Active).SingleOrDefault()
  </code></pre>
  
  <p>The above code executes ok when calling from a unit test. However, when calling from that method from a WCF web service, the following exception occurs:</p>
  
  <p><em>System.ServiceModel.FaultException: System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> 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. ---> System.ComponentModel.Win32Exception: The wait operation timed out</em></p>
  
  <p>Under the covers of Entity Framework, the below query is what is run when the query timed out. If I run the query it executes in less than a second. What might be causing this timeout?</p>
  
  <pre><code>exec sp_executesql N'SELECT TOP (2) 
  [Extent1].[Id] AS [Id], 
  [Extent1].[FirstName] AS [FirstName], 
  [Extent1].[LastName] AS [LastName], 
  [Extent1].[Email] AS [Email], 
  [Extent1].[Username] AS [Username], 
  [Extent1].[Password] AS [Password], 
  [Extent1].[AuthenticationKey] AS [AuthenticationKey], 
  [Extent1].[Active] AS [Active], 
  [Extent1].[Admin] AS [Admin], 
  [Extent1].[AutoSubmit] AS [AutoSubmit]
  FROM [dbo].[User] AS [Extent1]
  WHERE [Extent1].[Username] = @p__linq__0',N'@p__linq__0 varchar(8000)',@p__linq__0='test1'
  </code></pre>