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

1、安装SQLite

csharp 复制代码
在 nuget 搜索 System.Data.SQLite 安装

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

csharp 复制代码
 <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 文件

csharp 复制代码
<?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 文件

csharp 复制代码
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 文件 (数据模型)

csharp 复制代码
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 文件 (增删改查)

csharp 复制代码
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、调用

csharp 复制代码
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}");
            }

        }
    }
}
相关推荐
大飞pkz5 小时前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
唐青枫7 小时前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务16 小时前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther17 小时前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间18 小时前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec18 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#
Tiger_shl19 小时前
【层面一】C#语言基础和核心语法-02(反射/委托/事件)
开发语言·c#
mudtools1 天前
.NET驾驭Word之力:COM组件二次开发全攻略之连接Word与创建你的第一个自动化文档
后端·c#
王维志1 天前
LiteDB详解
数据库·后端·mongodb·sqlite·c#·json·database
程序猿多布1 天前
XLua教程之热补丁技术
unity·c#·lua·xlua