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

Sqlite EF CodeFirst For WPF

在WPF 开发中使用Sqlite 数据库的EF框架

1.安装必要的插件

  install-package System.Data.Sqliteinstall-package System.Data.Sqlite.EF6install-package Sqlite.CodeFirst

2.建立数据模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
namespace WpfApp4.Models
{public class Student{[Key]public int Id { get; set; }public string Name { get; set; }}
}

3.建立数据上下文

using SQLite.CodeFirst;
using System.Data.Entity;
namespace WpfApp4.Models
{public class MyDbContext: DbContext{public MyDbContext() : base("name=MyConnection") { }public DbSet<Student> Students { get; set; }protected override void OnModelCreating(DbModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);var init = new SqliteCreateDatabaseIfNotExists<MyDbContext>(modelBuilder);Database.SetInitializer(init);}}
}

4.配置文件

<?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><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" /><!--增加下面这行(复制上面一行,去掉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><!--增加下面的数据库连接配置信息--><connectionStrings><add name="MyConnection" connectionString="Data Source=.\mydb.db" providerName="System.Data.SQLite.EF6"/></connectionStrings>
</configuration>

5.使用方法

using System.Data.Entity.Infrastructure;
using System.Windows;
using WpfApp4.Models;namespace WpfApp4
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();using (var db = new MyDbContext()){db.Students.Add(new Student() {Name="Test1"});try{db.SaveChanges();MessageBox.Show("Success!");}catch (DbUpdateException ex){MessageBox.Show(ex.InnerException.Message);}}}}
}
  • 注意事项

(1)当前系统和sqlite 动态库版本要一致,如x86/x64

(2) 当前系统程序和使用的动态库版本要一致

http://www.hskmm.com/?act=detail&tid=39550

相关文章:

  • day02 pytorch介绍与安装
  • 拼多多一面
  • 大模型强化学习的熵控制:CE-GPPO、EPO与AsyPPO技术方案对比详解
  • 就是 CCPC2025 重庆站游记
  • 10.20-10.25 总结
  • Verilog学习-从FPGA的角度看Uart模块
  • 25秋周总结6
  • 20232313 2025-2026-1 《网络与系统攻防技术》实验三实验报告 - 20232313
  • 鸭子类型,反射
  • 2025 年 10 月承烧板厂家最新推荐,实力品牌深度解析采购无忧之选!
  • 朝花夕拾 -- AES(1)
  • 【学习笔记】构造
  • 特殊的背包问题
  • 【URP】Unity[视差贴图]模拟[冰面裂缝]实践
  • 10.26博客
  • 2025年热门的用地预审技术服务供应商、市面上用地预审技术服务公司、行业内用地预审技术服务品牌、市场用地预审技术服务方案、2025年用地预审技术服务单位深度解析
  • 2025年热门的用地预审技术服务供应商、市面上用地预审技术服务公司、行业内用地预审技术服务品牌、市场用地预审技术服务方案、2025年用地预审技术服务单位综合评测
  • 2025年热门的用地预审技术服务供应商、市面上用地预审技术服务公司、行业内用地预审技术服务品牌、市场用地预审技术服务方案、2025年用地预审技术服务单位综合排名与分析
  • 2025年市面上中压电缆品牌、行业内中压电缆实力厂家、口碑好的中压电缆公司、中压电缆公司推荐榜单、中压电缆厂家口碑排行榜
  • 2025年市面上中压电缆品牌、行业内中压电缆公司、口碑好的中压电缆产品、2025年中压电缆公司、中压电缆厂家推荐榜单全面解析
  • 2025年市面上中压电缆品牌、行业内中压电缆公司、口碑好的中压电缆产品、2025年中压电缆公司、中压电缆公司推荐权威排名
  • 2025年市面上中杆灯品牌、2025年中杆灯产品、口碑好的中杆灯公司、可靠的中杆灯优质厂家、中杆灯品牌推荐排行榜全面解析
  • 2025年市面上中杆灯品牌、行业内中杆灯优质厂家、口碑好的中杆灯公司、可靠的中杆灯产品、中杆灯供应厂家推荐排行榜深度评测
  • 2025年市面上中杆灯品牌、市面上中杆灯公司、口碑好的中杆灯品牌、可靠的中杆灯产品、中杆灯品牌推荐排行榜 top 10
  • 2025年市面上中杆灯品牌、行业内中杆灯优质厂家、国内中杆灯实力厂家、口碑好的中杆灯产品、中杆灯品牌推荐排行榜
  • 2025年口碑好的中杆灯公司与可靠的中杆灯产品推荐排行榜
  • 2025年西安楼盘品牌口碑推荐排行榜:热门的西安楼盘开发商精选
  • 2025年口碑好的双曲铝单板厂家、比较好的双曲铝单板供应商、靠谱的双曲铝单板公司、双曲铝单板产品排行榜、2025年双曲铝单板品牌前十强推荐
  • 2025年市面上双曲铝单板品牌、行业内双曲铝单板厂家、市场双曲铝单板产品、目前双曲铝单板供应商、口碑好的双曲铝单板公司综合评测
  • 高级语言程序设第二次个人作业