如何使用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)
            {
               
            }


        }


       


    }

}

结果如下:

相关推荐
廋到被风吹走1 天前
【数据库】【MySQL】InnoDB外键解析:约束机制、性能影响与最佳实践
android·数据库·mysql
掘根1 天前
【消息队列】交换机数据管理实现
网络·数据库
Logic1011 天前
《Mysql数据库应用》 第2版 郭文明 实验6 数据库系统维护核心操作与思路解析
数据库·sql·mysql·学习笔记·计算机网络技术·形考作业·国家开放大学
AI Echoes1 天前
构建一个LangChain RAG应用
数据库·python·langchain·prompt·agent
@nengdoudou1 天前
KingbaseES支持 mysql 的find_in_set函数
数据库·mysql
摇滚侠1 天前
面试实战 问题三十三 Spring 事务常用注解
数据库·spring·面试
梁萌1 天前
保姆级的MySQL执行计划(Explain)解读
数据库·mysql·explain·执行计划
JIngJaneIL1 天前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
+VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue图书管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
杨云龙UP1 天前
MySQL 8.0.x InnoDB 写入链路优化:Redo Log 与 Buffer Pool 扩容与缓冲区调优实战记录-20251029
linux·运维·数据库·sql·mysql