1

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">
5
  • What does the error say??? Commented Jun 4, 2013 at 16:34
  • You said the error message keeps changing, but you'll have to choose one. We can't guess at everything that could possibly be wrong. Commented Jun 4, 2013 at 16:36
  • @KingJulianHere is one Config Error The configuration section 'entityFramework' cannot be read because it is missing a section declaration Commented Jun 4, 2013 at 16:58
  • Config Source: 96: </runtime> 97: <entityFramework> 98: <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> Commented Jun 4, 2013 at 17:02
  • @ValOkafor. Do you have a <section name="entityFramework" inside your configSections? See my answer for the relevant bits from a sample config file. Commented Jun 4, 2013 at 17:09

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Andy, I have tried that without success and it is giving the same exact error message "The configuration section 'entityFramework' cannot be read because it is missing a section declaration". When you say exception details, do you mean the tracelog file that is pretty long to post.
@ValOkafor. The next most likely issue is that there is a problem somewhere else in the config file that is preventing it being read. Check your config file thoroughly. If you open the errors window in Visual Studio whilst in the config file, it might show you if you have an xml error.
@ValOkafor. And then, if the Xml is valid, you need to check there are no mistakes that are throwing the configuration readers, like an erroneous <clear /> tag for instance. So at that point, you might need to compare it to one from a fresh template from Visual Studio such as the MVC 4 internet application template and spot the difference.
Adding the config section did help, still having some errors but I was able to step through the error and get the application to run. I will now go line by line in the code and config file and compare with the book to make sure that there is no typo on my end. Your assistance is greatly appreciated. Thanks a lot
Thanks Andy. I also had incorrectly entityFramework before configSections. <configSections> must be before <entityFramework>
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.