精通.net framework6.0和sqlite的大佬帮忙看看
我想使用ef来操作sqlite数据库,但是一直连接不上,还一直报错,这是什么原因啊,求大佬帮忙看看(报错的详细信息在最下面)我不管百度还是用ai都没解决我是使用代码优先的模式https://t.tutu.to/img/kUPxw
https://t.tutu.to/img/kUNZ7
代码如下:
App.config
<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>
<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>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="BrushAndPracticeQuestionsConnectionStringName" connectionString="Data Source=.;Initial Catalog=BrushAndPracticeQuestions;Integrated Security=True;" providerName="System.Data.SqlClient" />
<add name="SqliteContext" connectionString="data source=.\Data\Plugin1.db;Version=3;" providerName="System.Data.SQLite.EF6" />
<!--//Version=3;-->
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="office" publicKeyToken="71e9bce111e9429c" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="16.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
数据库上下文类
public class SqLitePluginContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<SqLitePluginContext>(modelBuilder);
Database.SetInitializer(sqliteConnectionInitializer);
}
public SqLitePluginContext() : base("SqliteContext") { } //配置使用的连接名
public DbSet<Plugin> Plugin { get; set; }
}
模型类
public class Plugin
{
// 主键
// 自增
private int pluginID;
private string pluginName;
private string pluginVersion;
private DateTime pluginUpdateDate;
private string pluginAuthorName;
private string pluginIntroduce;
public string PluginName { get => pluginName; set => pluginName = value; }
public string PluginVersion { get => pluginVersion; set => pluginVersion = value; }
public DateTime PluginUpdateDate { get => pluginUpdateDate; set => pluginUpdateDate = value; }
public string PluginAuthorName { get => pluginAuthorName; set => pluginAuthorName = value; }
public string PluginIntroduce { get => pluginIntroduce; set => pluginIntroduce = value; }
public int PluginID { get => pluginID; set => pluginID = value; }
}
报错的信息
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
Source=EntityFramework
StackTrace:
在 System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
在 BrushQuestionsDAL.EntityFramework.SqLitePluginContext..ctor() 在 D:\backups\Code\Visual Studio\.Net\BrushQuestions\BrushQuestionsDAL\EntityFramework\SqLitePluginContext.cs 中: 第 25 行
在 BrushQuestionsBLL.Form.Testx.ccc() 在 D:\backups\Code\Visual Studio\.Net\BrushQuestions\BrushQuestionsBLL\Form\Testx.cs 中: 第 17 行
在 BrushQuestions.TestForm.button3_Click(Object sender, EventArgs e) 在 D:\backups\Code\Visual Studio\.Net\BrushQuestions\BrushQuestions\Application\System\Test\TestForm.cs 中: 第 54 行
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)
此异常最初是在此调用堆栈中引发的:
System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(bool)
System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(System.Configuration.ConfigurationSchemaErrors)
System.Configuration.ClientConfigurationSystem.EnsureInit(string)
内部异常 1:
ConfigurationErrorsException: Configuration system failed to initialize
内部异常 2:
ConfigurationErrorsException: Unrecognized configuration section system.data. (D:\backups\Code\Visual Studio\.Net\BrushQuestions\BrushQuestions\bin\Debug\net6.0-windows\BrushQuestions.dll.config line 7)
https://t.tutu.to/img/kUKti 我的项目是win form的
base那提示是配置使用的连接名,按理解base("SqliteContext")这里填的字符串是不是应该是base(“SQLite Data Provider (Entity Framework 6)”)之类的,否则程序怎么知道你使用的哪个配置项。除非是在约定大于配置的模式下。 15198743250 发表于 2024-9-4 00:10
我的项目是win form的
不管是winform还是web,首先肯定要保证有sqlite的对应库在搜索目录下存在 15198743250 发表于 2024-9-4 00:10
我的项目是win form的
sqlite 3.X版本支持不好,建议换低版本试试,尤其你还是winform平台,Microsoft.Data.Sqlite功能够用了,谈性能?你用winform交互UI都够呛。 framework6.0? 微软笑了.
别再用EF,难用死了。
换成sqlsugar 看看是不是DbContext构造函数入参错了
https://s2.loli.net/2024/09/04/TCE7gnkWFI9tQVp.png
https://learn.microsoft.com/zh-cn/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0 本帖最后由 typescript 于 2024-9-4 09:29 编辑
1. 没有.net framework6.0 这种称呼
2. 建议上.net 6或者8版本,ef core更好用
3.你nuget怎么加这么多?
建议放上整个项目代码或者github连接 这是codefirst的好处,一开始就劝退你,不是项目开发一半了,再去考虑换。
页:
[1]
2