请帮帮我,我已经为这个问题绞尽脑汁三天了。
我正在尝试编写一个带有内部数据库(SQLite)的WinForms应用程序,该数据库使用EntityFramework进行管理。问题是,当我尝试向表中添加记录时,出现以下异常:
“找不到具有固定名称“System.Data.SQLite”的 ADO.NET 提供程序的实体框架提供程序。请确保该提供程序已在应用程序配置文件的“entityFramework”部分中注册。请参阅http://go.microsoft。 com/fwlink/?LinkId=260882了解更多信息。”
这是 App.config 文件。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<entityFramework>
<providers>
<!-- Провайдер для SQL Server -->
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<!-- Провайдер для SQLite -->
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
<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="FinanceHelperConnection"
connectionString="Data Source=|DataDirectory|\FinanceHelperDB.sqlite;"
providerName="System.Data.SQLite.EF6"/>
</connectionStrings>
</configuration>
以下是应用程序中断的方法的代码:
private void createItemBtn_Click(object sender, EventArgs e)
{
if (itemNameField.Texts != "")
{
using (AppDbContext db = new AppDbContext())
{
Item newItem = new Item() { Name = itemNameField.Texts };
db.Items.Add(newItem);
db.SaveChanges();
reloadData();
}
}
else
{
MessageBox.Show("Стой, ты забыл заполнить поле 'Название'");
}
}
上下文类也是“经典”:
public class AppDbContext : DbContext
{
public AppDbContext() : base("name=FinanceHelperConnection")
{
}
public DbSet<Item> Items { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Transaction> Transactions { get; set; }
}
我已经通过管理器和控制台重新安装了库 100,500 次 - 它没有帮助(将它们更新到最新版本也没有帮助)。
这不是玩笑,但ChatGpt也挠头,以勤劳的啄木鸟的方式重复:“一切似乎都是正确的。检查你是否已经安装了库,如果需要,请重新安装它们。”请告诉我问题是什么以及如何解决?
因此,在审查了很多事情(显然不是所有事情 - 显然没有足够的时间、精力或机会来审查所有事情)之后,我得出的结论是……在旧版本的 . NET 框架,但你会厌倦拿着手鼓跳舞,然后用镇静剂把这一切都冲下去。默认情况下,通过 NuGet 安装必要的包后,App.config 文件中生成的内容是不正确的。您是否更正了 App.config 文件?好的,现在尝试启用交易,但请记住,您将无法执行此操作,因为提供商也需要交易。据我了解,这个问题的解决方案是在一个特定的包中,但它没有安装在旧版本的 .NET Framework 上。
结果,我只需要使用最新版本的.NET、EntityFramework.Core和相应版本的SQLite创建一个新项目,然后事情就从那里开始:提供商没有发现任何问题,交易顺利进行(好吧,几乎平静地,首先我必须安装一个额外的包),数据库被创建并且写入/读取没有引起任何问题。
我是否找到了有关如何与旧的 .NET Framework、EntityFramework 和 SQLite 交朋友的具体答案?唉,不。我是否找到了如何仍然获得有效应用程序的答案?也许。如果有人找到我第一个问题的答案,我会很高兴。
您需要自己重写该方法
DbContext.OnConfiguring。您的代码将如下所示:您可以从此处找到连接字符串。或者将数据库文件放置在项目根目录中的简化选项:
UPD: 为了使该方法
.UseSqlite可用,您还需要安装 NuGet 包Microsoft.EntityFrameworkCore.Sqlite