在WPF 开发中使用Sqlite 数据库的EF框架
1.安装必要的插件
install-package System.Data.Sqlite
install-package System.Data.Sqlite.EF6
install-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) 当前系统程序和使用的动态库版本要一致