关于C# 使用 sqlite 映射实体类笔记

1、安装SQLite

在 nuget 搜索 System.Data.SQLite 安装

2、在 app.conifg 文件中添加如下信息

 <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

解决问题:

No Entity Framework provider found for the ADO.NET provider with invariant name ‘System.Data.SQLite’. Make sure the provider is registered in the ‘entityFramework’ section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.”

完整的 app.config 文件

<?xml version="1.0" encoding="utf-8"?>
<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><startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /></startup><runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /><bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" /></dependentAssembly></assemblyBinding></runtime><entityFramework><providers><provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /><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" /></providers></entityFramework><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>
</configuration>

3、创建 SQLiteContext.cs 文件

using System.Data.Entity;
using System.Data.SQLite;namespace dbz_desktop_ser.db
{public class SQLiteContext : DbContext{public SQLiteConnection DbConnection { get; set; }public DbSet<NetbarInfo> NetbarInfo { get; set; }public SQLiteContext(string connectionString) : base(new SQLiteConnection() { ConnectionString = $@"URI=file:{connectionString}" }, true){DbConnection = (SQLiteConnection)base.Database.Connection;}}
}

4、创建 NetbarInfo.CS 文件 (数据模型)

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;namespace dbz_desktop_ser.dbModel
{[Table("NetbarInfo")]public class NetbarInfo{[Key]public int id { get; set; }[Column("name")]public string Name { get; set; }}
}

5、创建 NetbarInfoHelper.cs 文件 (增删改查)

using dbz_desktop_ser.dbModel;
using System.Linq;namespace dbz_desktop_ser.db
{public class NetbarInfoHelper{private readonly SQLiteContext _context;public NetbarInfoHelper(string connectionString){_context = new SQLiteContext(connectionString);}// 查询门店信息  public IQueryable<NetbarInfo> GetNetbarInfo(){return _context.NetbarInfo;}}
}

6、调用

using dbz_desktop_ser.db;
using System;
using System.Windows.Forms;namespace dbz_desktop_ser.WinForm
{public partial class FrmMain : Form{public FrmMain(){InitializeComponent();}private void FrmMain_Load(object sender, EventArgs e){var db = new NetbarInfoHelper("myConfig.db");var info = db.GetNetbarInfo();foreach (var item in info){Console.WriteLine($"id:{item.id} 名称:{item.Name}");}}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/641309.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL-SQL-DQL

DQL-介绍 DQL-语法 基本查询 1、查询多个字段 2、设置别名 3、去除重复记录 条件查询 1、语法 2、条件 聚合函数 1、介绍 2、常见的聚合函数 3、语法 分组查询 1、语法 2、where与having区别 排序查询 1、语法 2、排序方式 分页查询 1、语法 DQL-执行顺序

ubuntu 安装protobuf

apt 安装 sudo apt install protobuf-compiler 编译安装 – 方式1 资料链接&#xff1a;ubuntu环境 安装ncnn_ubuntu ncnn_jbyyy、的博客-CSDN博客 git clone https://github.com/google/protobuf.git cd protobuf git submodule update --init --recursive ./autogen.sh …

LeetCode59 螺旋矩阵 II

螺旋矩阵 II 循环不变量的应用 给你一个正整数 n &#xff0c;生成一个包含 1 到 n2 所有元素&#xff0c;且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。 示例 1&#xff1a; 输入&#xff1a;n 3 输出&#xff1a;[[1,2,3],[8,9,4],[7,6,5]] 示例 2&#xff1…

关于php8的数据类型转换

目录 1、数据类型介绍 1.1 简单数据类型&#xff1a; 1.2 复合数据类型&#xff1a; 1.3 特殊数据类型&#xff1a; 2、数据类型转换 2.1 自动转换&#xff1a; 2.2 强制&#xff08;手动&#xff09;转换&#xff1a; 3、验证数据类型途径 &#xff08;1&#xff09;var…

[自用代码]基于LSTM的广州车牌售价预测

文章目录 1. 数据2. 代码 明天是拍牌日了&#xff0c;用AI协助生成了一段时间序列预测代码&#xff0c;主要是用来预测明天车牌均价。其实做的挺low的&#xff0c;没有考虑多维数据之间的关系以及社会事件影响&#xff0c;仅仅是单步预测&#xff0c;偏差是挺大的&#xff0c;符…

2024年第3周农产品价格监测报告

一、摘要 农产品价格监测主要涉及对畜禽类产品、水产品、蔬菜类产品、水果类产品的价格&#xff0c;以周为单位&#xff0c;进行变化情况的数据监测。其中&#xff0c;蔬菜类产品共8种&#xff0c;分别为菜花、韭菜、豆角、西红柿、胡萝卜、土豆、大葱、葱头。 本周重点监测的…

WordPress函数has_tag的介绍及用法示例,判断是否含有指定标签?

我们很多WordPress站点的文章页都会添加相应的标签&#xff0c;在某些场合下我们需要判断当前文章页是否含有指定的标签&#xff0c;那么应该用什么判断函数呢&#xff1f;这个时候就需要用到WordPress函数has_tag()了&#xff0c;下面boke112百科就跟大家介绍一下这个函数及具…

Element组件完整引入、按需引入、样式修改(全局、局部)、简单安装less以及npm命令证书过期等

目录 一、npm 安装二、完整引入三、按需引入四、样式修改1.按需加载的全局样式修改2. 局部样式修改1. 在 css 预处理器如 less scss 等直接使用::v-deep2. 只能用在原生 CSS 语法中:/deep/ 或者 >>> 五、 拓展&#xff1a;npm 安装less报错&#xff0c;提示证书过期六…

斯拉、理想等车企大幅降价,新一轮大洗牌正在进行中 |百能云芯

2024年刚开年&#xff0c;各大车企就先后抛出降价大礼包&#xff01;1月1日&#xff0c;特斯拉率先宣布调价&#xff0c;推出Model 3后驱现车保险补贴及低息金融政策&#xff0c;总优惠幅度高达2.2万元。正当大家还没有完全消化完特斯拉的调价信息后&#xff0c;理想汽车也宣布…

C# 判断文件路径的后缀是否是某个后缀

C# 判断文件路径的后缀是否是某个后缀&#xff0c;通过解析文件名并检查其扩展名来判断文件的后缀是否为.dcm。 以.dcm为例子&#xff0c;代码如下&#xff1a; 直接判断&#xff1a; using System; using System.IO; class Program { static void Main() { string …

美国大带宽服务器租用需要考虑的因素

很多用户会选择租用服务器&#xff0c;那么美国大带宽服务器租用时应该考虑哪些因素呢?小编为您整理发布美国大带宽服务器租用考虑因素。 在美国租用大带宽服务器时&#xff0c;您可以考虑以下几个因素&#xff1a; 1. **性能配置**&#xff1a;选择服务器时&#xff0c;应确保…

Unity中URP下的 额外灯 逐像素光 和 逐顶点光

文章目录 前言一、额外灯 的 逐像素灯 和 逐顶点灯1、存在额外灯的逐像素灯2、存在额外灯的逐顶点灯 二、测试这两个宏的作用1、额外灯的逐像素灯2、额外灯的逐顶点灯 前言 在之前的文章中&#xff0c;我们了解了 主光相关的反射计算。 Unity中URP下的SimpleLit的 Lambert漫反…

有效的数独[中等]

优质博文&#xff1a;IT-BLOG-CN 一、题目 请你判断一个9 x 9的数独是否有效。只需要根据以下规则&#xff0c;验证已经填入的数字是否有效即可。 数字 1-9 在每一行只能出现一次。 数字 1-9 在每一列只能出现一次。 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一…

Elasticsearch:介绍 kNN query,这是进行 kNN 搜索的专家方法

作者&#xff1a;来自 Elastic Mayya Sharipova, Benjamin Trent 当前状况&#xff1a;kNN 搜索作为顶层部分 Elasticsearch 中的 kNN 搜索被组织为搜索请求的顶层&#xff08;top level&#xff09;部分。 我们这样设计是为了&#xff1a; 无论分片数量多少&#xff0c;它总…

云平台性能测试之网络性能测试

一、认识网络性能测试 网络性能测试是评估和测量计算机网络性能的过程&#xff0c;对于确保网络的有效运行和提供良好用户体验非常重要。网络性能测试可以实现以下目标&#xff1a; 性能评估&#xff1a; 网络性能测试可以帮助评估网络的整体性能。通过测量网络的带宽、延迟、…

python 面经

关于自身特点 1. 介绍下自己&#xff0c;讲一下在公司做的项目 2. 说一下熟悉的框架&#xff0c;大致讲下其特点 python 基础 1.可变与不可变类型区别 2.请解释join函数 3.请解释*args和**kwargs的含义&#xff0c;为什么使用* args&#xff0c;** kwargs&#xff1f; 4.解释…

实现纯Web语音视频聊天和桌面分享(附源码,PC端+移动端)

在网页里实现文字聊天是比较容易的&#xff0c;但若要实现视频聊天&#xff0c;就比较麻烦了。本文将实现一个纯Web版的视频聊天和桌面分享的Demo&#xff0c;可直接在浏览器中运行&#xff0c;不需要安装任何插件。 一. 主要功能及支持平台 1.本Demo的主要功能有 &#xff…

【书生·浦语】大模型实战营——第六次作业

使用OpenCompass 评测 InterLM2-chat-chat-7B 模型在C-Eval数据集上的性能 环境配置 1. 创建虚拟环境 conda create --name opencompass --clone/root/share/conda_envs/internlm-base source activate opencompass git clone https://github.com/open-compass/opencompass cd…

valgrind安装使用教程

安装 valgrind官网下载链接 安装过程&#xff1a; tar -xf valgrind-3.22.0.tar.bz2 cd valgrind-3.22.0/ ./configure make make install使用 基本工具 Memcheck&#xff0c;检测内存错误使用问题Callgrind&#xff0c;检查程序函数调用过程中出现的问题Cachegrind&#x…

LED闪烁

这段代码是用于STM32F10x系列微控制器的程序&#xff0c;主要目的是初始化GPIOA的Pin 0并使其按照特定的模式进行闪烁。下面是对这段代码的逐行解释&#xff1a; #include "stm32f10x.h"&#xff1a;这一行包含了STM32F10x系列微控制器的设备头文件。这个头文件包含…