title: Calling EF Code First from Windows Service
body: <p>I'm trying to do a simple select system in the <code>OnStart()</code> event of a Windows service.But as soon as I introduce the code that talks to the database to the <code>OnStart()</code> event I get the following error:</p>
  
  <blockquote>
    <p><em>An exception occurred while initializing the database. See the InnerException for details. The underlying provider failed on Open.</em></p>
  </blockquote>
  
  <p><strong>C# code:</strong></p>
  
  <pre><code>protected override void OnStart(string[] args)
  {
      try
      {
          string serverName = GetServerName();
          string serverIp = GetServerIP();
  
          //This is the line that causes the exception
          var dbContext = new DatabaseContext();
  
          bool serverExists = dbContext.Server.Any(s =&gt; s.ServerName == serverName &amp;&amp; s.ServerIp == serverIp);
  
          if (!serverExists)
          {
              //Add server details to the DB
          }
      }
      catch (Exception eX)
      {
          using (var w = new StreamWriter("C:\\ErrorFile.txt", true))
          {
              w.WriteLine(DateTime.Now.ToString("dd MM yyyy HH:mm:ss"));
              w.WriteLine(eX.Message);
              if (eX.InnerException != null)
                  w.WriteLine(eX.InnerException.Message);
              w.WriteLine(eX.StackTrace);
              w.WriteLine();
              w.Close();
          }
      }
  }
  </code></pre>
  
  <p><strong>App.config:</strong></p>
  
  <pre><code>&lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
    &lt;/configSections&gt;
    &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
    &lt;connectionStrings&gt;
      &lt;add name="ServiceManagerConnection" providerName="System.Data.SqlClient" connectionString="Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=ServiceManager;Data Source=." /&gt;
    &lt;/connectionStrings&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
    &lt;/startup&gt;
    &lt;appSettings&gt;
      &lt;add key="ServiceRunInterval" value="5" /&gt;
    &lt;/appSettings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>Stack trace:</strong></p>
  
  <pre><code>  at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
     at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
     at System.Data.Entity.Internal.LazyInternalContext.&lt;InitializeDatabase&gt;b__4(InternalContext c)
     at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
     at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
     at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
     at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
     at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
     at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
     at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
     at System.Linq.Queryable.Any[TSource](IQueryable`1 source, Expression`1 predicate)
     at mySuperSecretProjectName.Program.OnStart(String[] args) in c:\mySuperSecretProjectName\Program.cs:line 49
  </code></pre>