当前位置: 首页 > 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.zskr.cn/news/31003.html

相关文章:

  • 2025年热门的用地预审技术服务供应商、市面上用地预审技术服务公司、行业内用地预审技术服务品牌、市场用地预审技术服务方案、2025年用地预审技术服务单位综合排名与分析
  • 2025年市面上母线槽品牌、供应厂家、口碑产品、品牌排行及可靠品牌综合评测
  • 2025年市面上中压电缆品牌、行业内中压电缆实力厂家、口碑好的中压电缆公司、中压电缆公司推荐榜单、中压电缆厂家口碑排行榜
  • 2025年市面上中压电缆品牌、行业内中压电缆公司、口碑好的中压电缆产品、2025年中压电缆公司、中压电缆厂家推荐榜单全面解析
  • 2025年市面上中杆灯品牌、行业内中杆灯优质厂家、口碑好的中杆灯公司、可靠的中杆灯产品、中杆灯供应厂家推荐排行榜深度评测
  • 2025年市面上中杆灯品牌、行业内中杆灯优质厂家、国内中杆灯实力厂家、口碑好的中杆灯产品、中杆灯品牌推荐排行榜
  • 2025年餐桌石材品牌综合评测:市面上品牌、制造厂、口碑产品与推荐榜单
  • 20232311 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 2025年市面上餐桌石材品牌、行业内餐桌石材制造厂、口碑好的餐桌石材产品、餐桌石材品牌推荐、2025年餐桌石材品牌综合评测
  • 2025年市面上餐桌石材品牌、行业内餐桌石材制造厂、口碑好的餐桌石材产品、餐桌石材品牌推荐、2025年餐桌石材产品深度解析
  • 2025年市面上防火桥架品牌、国内防火桥架产品、2025年防火桥架源头厂家、口碑好的防火桥架供应厂家、防火桥架品牌推荐权威解析
  • 用观察优化枚举
  • [Ubuntu] Ubuntu24.04 apt安装时被自动更新进程锁定
  • [Ubuntu] Ubuntu24.04安装Intel显卡驱动OneVPL
  • Python图表库Matplotlib 组成部分介绍(Good)
  • so文件找不到却可以使用的解决
  • 继承与多态动手动脑 - 20243867孙堃2405
  • pyd逆向处理
  • 太突然!湘潭大学计算机学院刘昊霖教授不幸逝世,年仅37岁。
  • 2025年摩托车厂家权威推荐榜:覆盖街车、跑车、巡航车及越野车型的全方位选购指南与实力解析
  • AIGC图片视频制作通用提示词 - 详解
  • 2025年包装机厂家权威推荐榜单:全自动包装机,真空包装机,食品包装机,立式包装机源头厂家综合实力解析
  • ssh: 连接报错
  • 实用指南:Golang 中的字符串:常见错误和最佳实践
  • OI 笑传 #22
  • 2025长沙1024程序员日:为开发者职业发展插上腾飞之翼
  • 2025东莞包装机/自动包装机/半自动包装机厂家推荐垚林机械,精准高效耐用!
  • MX-S 10-25 比赛总结
  • 7天阅读betaflight
  • 基于Sharding-JDBC的读写分离