如何使用EF框架操作Sqlite

1.下载相关的NuGet

2.创建如下的项目结构

3.创建AppDbContext类

cs 复制代码
using Microsoft.EntityFrameworkCore;
using SaveSyetemConfiguration.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SaveSyetemConfiguration.DBContext
{
    public class AppDbContext : Microsoft.EntityFrameworkCore.DbContext
    {
        // 数据库连接字符串(动态创建时可通过参数传入)
        private readonly string _connectionString;

        // 构造函数接收连接字符串(支持动态配置)
        public AppDbContext(string connectionString)
        {
            _connectionString = connectionString;
        }

        // 实体集(对应数据库表)
        public Microsoft.EntityFrameworkCore.DbSet<PerSon> PersonData { get; set; } = null;

        // 重写 OnConfiguring 配置数据库选项
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlite(_connectionString); // 使用 SQLite 提供程序
            }
        }
    }
}

4.创建对应的Model类

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SaveSyetemConfiguration.Model
{
    public class PerSon
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }


    }
}

5.在程序类进行简单的操作

cs 复制代码
using SaveSyetemConfiguration.DBContext;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SaveSyetemConfiguration
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string DbPath = @"D:\NowText";
            string connectionString = "通道1";
            try
            {
                Directory.CreateDirectory(DbPath);
                connectionString = $"Data Source={DbPath}\\{connectionString}.db";
                AppDbContext Db = new AppDbContext(connectionString);
                Db.Database.EnsureCreated();

                Db.PersonData.Add(new Model.PerSon()
                {
                    Age = 1,
                    Name = "张三"
                });
                Db.PersonData.Add(new Model.PerSon()
                {
                    Age = 1,
                    Name = "李四"
                });
                var count = Db.SaveChanges();
            }
            catch (Exception)
            {
               
            }


        }


       


    }

}

结果如下:

相关推荐
阳光九叶草LXGZXJ3 小时前
达梦数据库-报错-15-列【XXX】长度超出定义
linux·运维·数据库·sql·学习
字节渡客4 小时前
Redis键明明过期了,业务还在读到旧数据
数据库·redis·spring
OnlineProxy6 小时前
亚马逊多账号运营:如何在规避“关联封号”风险的同时实现电商规模化扩张
服务器·数据库·redis
辻弋2018 小时前
五年前的旅行视频糊成马赛克?Video2X用Real-ESRGAN逐帧重建细节,但只支持Windows、集显用户建议直接放弃
服务器·数据库·windows·游戏引擎·电脑
可乐鸡翅yeah_9 小时前
业务中 M3U8 水印相关坑,硬水印和动态水印区别
前端·网络·数据库·ffmpeg·m3u8在线
石头麻辣鱼9 小时前
Bamboo 调度系统 OceanBase 适配实战:存储过程迁移踩过的三个大坑
数据库
龙亘川9 小时前
数智驱动民政升级 精准守护民生保障
大数据·数据库·人工智能
念何架构之路9 小时前
zap扩展生态与总结
java·前端·数据库
晚安日记wanna10 小时前
MySQL 回表为什么这么慢?5 种优化手段逐个拆解
数据库·mysql·性能优化
第七页独白10 小时前
汽车零件厂如何通过 QMS 真正落地 IATF 16949——QMS软件系统:品质检验-内审稽核-8d客诉管理:全星质量管理软件系统
java·前端·数据库