Val Okafor
Val Okafor

Reputation: 3437

Entity Framework 5 with Code First Connection String Error

Alright, I have googled this all morning and I can really use some help. I am following along a book by Adam Freeman (Pro ASP.Net MVC 4) and I am stuck in Chapter 7. BTW, I am not sure why Apress does not have support forum like Wrox where authors can help people get unstuck with examples in their book.

Anyway, the book used a database first to EF, following the book, I created a localDB, defined DB schema and added some sample data.Then created this DBcontext

    using System.Data.Entity;
    using SportsStore.Domain.Entities;

    namespace SportsStore.Domain.Concrete
   {
       class EFDbContext : DbContext
   {
       public DbSet<Product> Products { get; set; }
   }
   }

And then here is the connection string

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=(localdb)\v11.0;Initial     Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

And also, here is some settings that I guess was auto added by EF/Nuget during installation

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

The error message is all over the place, as I keep messing wit it, the error messages keep changing but they all point to something about Entity Framework. Please help, any assistance is greatly appreciated so I can proceed with my self study.

The current error message is "The configuration section 'entityFramework' cannot be read because it is missing a section declaration"

Config Source:
   96:   </runtime>
   97:   <entityFramework>
   98:     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">

Upvotes: 1

Views: 7811

Answers (2)

Jaryn
Jaryn

Reputation: 486

I used ASP.NET Core 1.0 RC1. For me it didn't work because of web.config. The problem was with web.config file. At the beginning my config file looks like that:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
  </system.webServer>
  <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>
</configuration>

To resolve this problem there are two ways. First is to add into web.config lines as Andy Brown shown. Be aware about diffrent version of EF.

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
</configuration>

Second is to remove whole entityFramework section.

Upvotes: 0

Andy Brown
Andy Brown

Reputation: 19161

To try and get a handle on the error, could you specify your connection string name in the constructor:

using System.Data.Entity;
using SportsStore.Domain.Entities;

namespace SportsStore.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public EFDbContext() : base("EFDbContext") {}
        public DbSet<Product> Products { get; set; }
    }
}

make sure that the string you pass in for the name matches the "name" attribute in your web.config

<connectionStrings>
    <add name="EFDbContext" connectionString="Data Source=(localdb)\v11.0;Initial     Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

If that doesn't work, try using the "name=" addition, as below (useful reference here). This should force EF5 to throw an error you can use for diagnostics if it doesn't find the connection string in the config file.:

namespace SportsStore.Domain.Concrete
{
    public class EFDbContext : DbContext
    {
        public EFDbContext() : base("name=EFDbContext") {}
        public DbSet<Product> Products { get; set; }
    }
}

If that doesn't work, then we'll need some exception details from you.

EDIT:

"The configuration section 'entityFramework' cannot be read because it is missing a section declaration"

Your entityFramework section should look like this, be careful that it is a direct child of the element:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- other section and sectionGroup declarations -->
  </configSections>
  <!-- other sections -->
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <!-- other sections -->
</configuration>

Upvotes: 3

Related Questions