首先安装nuget包:System.Data.SQLite 和 SQLite.CodeFirst,如下二图:
然后在App.config中配置数据库连接字符串:
<connectionStrings>
<add name="StuDB" connectionString="data source=BoilerCalculator.db" providerName="System.Data.SQLite.EF6" />
</connectionStrings>
下面用这种方式实现WPF对学生信息的数据管理(效果图如下)
一:创建实体类Students
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace EFCoreDemo.Models
{
[Table("StudentManager")]//映射SqLite的表格
public class Students
{
//学生ID
public int Id { get; set; }
//学生名称
public string StudentName { get; set; }
//学生性别
public string Gender { get; set; }
//学生出生年月
public DateTime Birthday { get; set; }
//学生地址
public string Address { get; set; }
//家长电话
public string PatriarchTel { get; set; }
}
}
二:studentContext数据库关系映射类:
using EFCoreDemo.Models;
using SQLite.CodeFirst;
using System.Data.Entity;
namespace EFCoreDemo
{
public class StudentContext:DbContext
{
public StudentContext() : base("StuDB")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var intit = new SqliteCreateDatabaseIfNotExists<StudentContext>(modelBuilder);
Database.SetInitializer(intit);
Database.SetInitializer(new SqliteDropCreateDatabaseWhenModelChanges<StudentContext>(modelBuilder));
modelBuilder.Entity<Students>();
base.OnModelCreating(modelBuilder);
}
//映射类
public DbSet<Students> StudentsData { get; set; }
}
}
三:封装的增删改查方法类
using EFCoreDemo.DB;
using EFCoreDemo.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace EFCoreDemo
{
public static class EFCoreHelper
{
private static StudentContext _studentContext = new StudentContext();
public static StudentContext studentContext { get => _studentContext; set => _studentContext = value; }
//查询所有学生数据
public static ObservableCollection<Students> GetStudents()
{
var list=studentContext.StudentsData.ToList();
return new ObservableCollection<Students>(list);
}
//添加新的学生信息
public static int InsertStu(Students students)
{
try
{
studentContext.StudentsData.Add(students);
studentContext.SaveChanges();
return 0;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return -1;
}
}
//修改学生信息
public static int UpdateStu(Students students)
{
try
{
var id = studentContext.StudentsData.Find(students.Id);
var entry = studentContext.Entry(id);
entry.CurrentValues.SetValues(students);
entry.Property(p => p.Id).IsModified = false;
studentContext.SaveChanges();
return 0;
}
catch
{
return -1;
}
}
//删除学生信息
public static void RemoveStu(int ID)
{
Students student = GetStudentWithID(ID);
studentContext.StudentsData.Remove(student);
studentContext.SaveChanges();
}
//学生信息关键字查询
public static List<Students> CheckStudentWithKey(string StuName)
{
return studentContext.StudentsData.Where(p => p.StudentName == StuName).ToList();
}
//学生信息ID查询
public static Students GetStudentWithID(int ID)
{
return studentContext.StudentsData.FirstOrDefault(p => p.Id == ID);
}
}
}
之后就可以调用封装类EFCoreHelper里的方法完成增删改查了。具体调用方法参考以下链接。不同之处是原博主使用的是SQLServer数据库,我这边使用的是SQLite数据库,其它都可以参考以下链接的文章。
https://blog.csdn.net/m0_47274301/article/details/118878005