2
votes

I have 2 projects: class library(as DAL layer) and ASP.NET MVC project(as UI). For getting data I try to use EF6, but it doesn't work. All the exception text:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file.

In DAL app.config I have this:

<entityFramework>
     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory,
 EntityFramework">
       <parameters>
         <parameter value="mssqllocaldb" />
       </parameters>
     </defaultConnectionFactory>
     <providers>
       <provider invariantName="System.Data.SqlClient"
                 type="System.Data.Entity.SqlServer.SqlProviderServices,
                       EntityFramework.SqlServer" />
     </providers>   
</entityFramework>

EF generates connection string with sqexpress provider:

Data Source=.\SQLEXPRESS;Initial Catalog=DAL.Entity.DataContext;Integrated Security=True;MultipleActiveResultSets=True

P.S. Please, help, I'm going to hate this damn thing.

2
The section that you reference in your DAL app.config - is it present in your web.config for your UI project? When you compile, the UI project becomes the primary assembly with a reference to the DAL assembly. As such, the connection string information will come from the primary (UI) assembly configuration -> the DAL config is never referenced. If that section does not exist there, you will get an error.Tommy
You will need both the EntityFramework section as well as the ConnectionString setting.Tommy
@Tommy But then I should install EF to UI project, it's not goodJulia
You don't have to install it to the UI project, but you have to have the configuration information in the primary assemblies configuration file. That is the only way for EF to get its configuration information.Tommy
@Tommy Ok, i add all the entity framework section to web.config, now I have this: HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.Julia

2 Answers

2
votes

In order for your DAL project to have the proper configuration setup, you have to include the EntityFramework information in your web.config. The reason for this is the UI project is the primary assembly and as such, all configuration information must come from its configuration file. When you compile, referenced assembly configuration files are not used for configuration, only the primary configuration file.

With that said, here is the bare minimum web.configuration file for using EntityFramework. Ensure that you have the tags nested correctly within the configuration file.

    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DAL.Entity.DataContext;Integrated Security=True;MultipleActiveResultSets=True" />
      </connectionStrings>
      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
    </configuration>

Note that I am not saying to copy/paste this, but rather look at the element levels and ensure you have the nesting levels correct in your web.config. Having elements out of order will cause a 500.19 configuration error.

1
votes

I was having similar issue and this method partially worked. It was selecting "SQLExpress" and was throwing connection error.

After looking carefully I found that another layer was "Set as Startup Project" instead of ASP.NET MVC project(as UI).

Changed "Set as Startup Project" to ASP.NET MVC project(as UI) and it worked like a charm.