WPF 和 EF6 - 无法识别的配置部分 entityFramework

WPF and EF6 - Unrecognized configuration section entityFramework

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

我正在按照这个 Microsoft official tutorial 创建一个代码优先的应用程序,使用 SQLite 称为 WPF_EF6。使用如下所示的配置,我收到一个错误。

问题:我可能在这里遗漏了什么?我们如何解决这个问题?应用程序编译正常但抛出此运行时错误:

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; }
    }
}

当您安装 System.Data.SQLite.EF6 Nuget 包时,App.config 中有一些现在遗漏的更改。准确地说,您缺少 <configSections> 元素。

你的 App.config 应该这样开始:

<?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>
    <!-- ... -->