当前位置: 首页 > article >正文

.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; }
 

    }
}


http://www.kler.cn/a/590933.html

相关文章:

  • Shell脚本中的弱治简写
  • 紧急通知:某平台泄露充电桩财富公式!5台×120kW=1.3年回本,年利润34.3万!速删前收藏 - 慧知开源充电桩平台
  • 234.回文链表
  • 【机器人-基础知识】标定 - IMU(Inertial Measurement Unit, 惯性测量单元)
  • Go语言的负载均衡
  • MyBatis-Plus防全表更新与删除插件BlockAttackInnerInterceptor
  • 微信小程序订阅消息发送消息,点击消息进入小程序页面
  • 4.玩转热图(相关矩阵、缺失值、多维相关、聚类热图、时间序列)——Python数据挖掘代码实践
  • 数据结构概览
  • Python的Pytest(2)
  • vulhub/joker 靶机----练习攻略
  • pycharm-python國際象棋遊戲代碼
  • C语言 论static和extern关键字
  • 透析 HTTP OPTIONS 预检请求
  • 软考中级-数据库-5.4 信息安全与网络安全
  • TCP 通信流程图
  • 使用pyinstaller打包py文件
  • 网络编程套接字【端口号/TCPUDP/网络字节序/socket编程接口/UDPTCP网络实验】
  • [Java微服务架构]1_架构选择
  • RISCV虚拟化环境搭建