.NET8使用EF Core连接SQLite
使用框架 .NET8
在nuget中,需要安装包:
SQLitePCLRaw.bundle_e_sqlite3,版本 2.1.10
Microsoft.EntityFrameworkCore.Sqlite.Core,版本 9.0.0
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.Reflection.Metadata;
using WpfRepairProject.Model;
namespace WpfRepairProject.IData_impl
{
public class EFContextSQLite : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
try
{
string sqlitefileName = WpfSnqkGasAnalysis.Model.MyConfigReader.GetConfigValue("connctionDb");
string filePath = Path.Combine(AppContext.BaseDirectory, "DataBase", sqlitefileName);
#if DEBUG
filePath = "G:\\work_my_test\\推荐表单\\linjie_recommend_project\\代码\\snqk_wpf_qi\\WpfSnqkGasAnalysis\\WpfSnqkGasAnalysis\\DataBase\\ljRepaireProject.sqlite";
#endif
options.UseSqlite($"Data Source={filePath}");
//设置不跟踪所有查询
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
#if DEBUG
//启用敏感数据日志记录
options.EnableSensitiveDataLogging();
//记录日志
options.LogTo(msg =>
{
//调试-窗口消息
System.Diagnostics.Debug.WriteLine(msg);
//输出-窗口消息
Console.WriteLine(msg);
}, Microsoft.Extensions.Logging.LogLevel.Information);
#endif
// options.
}
catch (Exception ex)
{
ex = ex.InnerException ?? ex;
LogHelpter.AddLog("数据库连接异常," + ex.Message, "error_DbContext");
}
}
public DbSet<K_department> K_department { get; set; }
public DbSet<K_user> K_user { get; set; }
public DbSet<K_login_log> K_login_log { get; set; }
public DbSet<K_operate_log> K_operate_log { get; set; }
public DbSet<K_repair_classify> K_repair_classify { get; set; }
public DbSet<K_repair_project> K_repair_project { get; set; }
public DbSet<K_repair_recommend_main> K_repair_recommend_main { get; set; }
public DbSet<K_repair_recommend_result> K_repair_recommend_result { get; set; }
public DbSet<K_service_department> K_service_department { get; set; }
}
}