title: Entity Framework 6 and Custom Controls
body: <p>I'm creating a custom user control that will eventually be used in multiple projects.  It's going to be a fat control that contains a lot of business logic and accesses the SQL database to load data and validate user actions.  (One of my requirements is to make it as simple as possible to drop it onto a new form.)  So I have a "Windows Forms Control Library" project with a user control in it.  And I'm setting up an edmx with Entity Framework 6.  I write a LINQ query against my data model that returns a few rows of data and I use that data to populate a combo box.  Now I want to test that out.  So I add a WinForms project to the solution, add my control to Form1 and run the test project.  I get the following error:</p>
  
  <blockquote>
    <p>No connection string named 'MyDatabaseEntities' could be found in the
    application config file.</p>
  </blockquote>
  
  <p>Right... I need to update the App.config with all that EF6 configuration.  So I copy the bulk of my App.Config from my control library project into the App.Config for my WinForms test project and I end up with:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &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;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
    &lt;/configSections&gt;
    &lt;connectionStrings&gt;
      &lt;add name="MyDatabaseEntities" connectionString="metadata=res://*/MyDatabase.csdl|res://*/MyDatabase.ssdl|res://*/MyDatabase.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=MyServer;initial catalog=MyDatabase;persist security info=True;user id=testuser;password=testpassword;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;
    &lt;/connectionStrings&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
    &lt;/startup&gt;
    &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"&gt;
        &lt;parameters&gt;
          &lt;parameter value="v11.0" /&gt;
        &lt;/parameters&gt;
      &lt;/defaultConnectionFactory&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;/configuration&gt;
  </code></pre>
  
  <p>Now, when I run the test project I get this error:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.InvalidOperationException'
    occurred in mscorlib.dll</p>
    
    <p>Additional information: The Entity Framework provider type
    'System.Data.Entity.SqlServer.SqlProviderServices,
    EntityFramework.SqlServer' registered in the application config file
    for the ADO.NET provider with invariant name 'System.Data.SqlClient'
    could not be loaded. Make sure that the assembly-qualified name is
    used and that the assembly is available to the running application.
    See <a href="http://go.microsoft.com/fwlink/?LinkId=260882" rel="nofollow">http://go.microsoft.com/fwlink/?LinkId=260882</a> for more
    information.</p>
  </blockquote>
  
  <p>I can fix this by adding EF6 to the Test Project, but that doesn't seem right.  I want my control to encapsulate all of the data access.  As long as I have the EF configuration information in the App.Config, that should be enough, right?  But there's obviously something missing that gets fixed if I add EF to the test project.  Can anyone tell me what else I need to do to the test project, short of adding EF?  (Or is there a better way, entirely, to do what I'm shooting for?)</p>
  
  <p>thanks!</p>