You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将带SQLite的WPF应用从.NET 4.5迁移到.NET 5运行异常如何解决

问题排查与解决方案

直接问题修复

报错提示的Unrecognized configuration section system.web是触发异常的直接原因:system.web是.NET Framework专属的配置节点,.NET 5的配置系统完全不识别该节点,直接删除即可:

  • 打开项目根目录下的App.config文件,找到完整的<system.web>...</system.web>节点,整段删除。

后续排查步骤

如果删除该节点后仍有报错,按以下顺序排查:

  1. 核对EF版本兼容性
    如果你仍在使用Entity Framework 6,必须升级到6.4及以上版本才支持.NET 5,低版本EF6无法在.NET Core/.NET 5+环境下运行。如果切换到EF Core,需要移除所有原EF6的NuGet引用,安装对应版本的Microsoft.EntityFrameworkCore.Sqlite包。
  2. 核对EF配置
    • 保留使用EF6的场景:确保App.config里的EF配置节版本匹配,示例配置如下:
      <configSections>
        <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="你的DbContext子类名称" connectionString="Data Source=你的SQLite数据库文件路径;Version=3;" providerName="System.Data.SQLite.EF6" />
      </connectionStrings>
      <entityFramework>
        <providers>
          <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
        </providers>
      </entityFramework>
      
    • 切换到EF Core的场景:无需在App.config中保留EF相关配置,直接在DbContext的OnConfiguring方法中指定连接字符串即可:
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseSqlite("Data Source=你的SQLite数据库文件路径");
      }
      
  3. 清理残留缓存
    删掉项目目录下的bin、obj文件夹,清理NuGet缓存后重新生成项目,避免旧版本的配置文件残留被加载。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.23 16:57:02