Search code examples
c#wpfentity-frameworkentity-framework-6ef-code-first

WPF and EF6 - Unrecognized configuration section entityFramework


Environment: VS2019, .NET 4.8, EF 6, SQLite, WPF App (.NET Framework)

I am following this Microsoft official tutorial to create a code-first app, called WPF_EF6 using SQLite. With the configuration shown below, I am getting an error.

Question: what may I be missing here? And how can we resolve the issue? App compiles fine but throws this runtime error:

ConfigurationErrorsException: Unrecognized configuration section entityFramework. (C:\DotNet2019\WPF\WPF_EF6\bin\Debug\WPF_EF6.exe.Config line 22)

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SQLite.EF6" />
            <add name="SQLite Data Provider (Entity Framework 6)"
                 invariant="System.Data.SQLite.EF6"
                 description=".NET Framework Data Provider for SQLite (Entity Framework 6)"
                 type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
            <remove invariant="System.Data.SQLite" />
            <add name="SQLite Data Provider" invariant="System.Data.SQLite"
                 description=".Net Framework Data Provider for SQLite"
                 type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
        </DbProviderFactories>
    </system.data>
    <connectionStrings>
        <add name="SqlLiteContext" 
             connectionString="Data Source=|DataDirectory|MySQLiteDb.sqlite" 
             providerName="System.Data.SQLite" />
    </connectionStrings>
    <entityFramework>
        <providers>
            <provider invariantName="System.Data.SQLite.EF6"
                      type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
            <provider invariantName="System.Data.SqlClient"
                      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="System.Data.SQLite"
                      type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
        </providers>
    </entityFramework>
</configuration>

MyDbContex.cs:

namespace WPF_EF6.Model
{
    public class MyDbContex : DbContext
    {
        public MyDbContex() : base("MySQLiteDb")
        {
        }

        public DbSet<MyObj> MyObjs { get; set; }
    }
}

Solution

  • When you install System.Data.SQLite.EF6 Nuget package, there was some changes in App.config that is missed right now. To be precise, your are missing <configSections> element.

    Your App.config should start like this:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
        </configSections>
        <!-- ... -->