我想使用ef来操作sqlite数据库,但是一直连接不上,还一直报错,这是什么原因啊,求大佬帮忙看看(报错的详细信息在最下面)我不管百度还是用ai都没解决我是使用代码优先的模式
代码如下:
App.config
[C#] 纯文本查看 复制代码 <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>
数据库上下文类
[Asm] 纯文本查看 复制代码
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; }
}
模型类
[Asm] 纯文本查看 复制代码
public class Plugin
{
[Key]// 主键
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]// 自增
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; }
}
报错的信息
[Asm] 纯文本查看 复制代码 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)
|