本文主要是介绍『SQLite』C#/.NET EF+SQLite.CodeFirst——真正实现CodeFirst,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
📣读完这篇文章里你能收获到
- 本文详细介绍了SQLite实现CodeFirst的方式
- 通过代码配合图文的形式进行讲解
- 这篇文章强烈建议收藏!!!免得下次找不到
文章目录
- 一、提前准备
- 1 下载Sqlite Expert
- 2 安装VS扩展工具
- 二、项目接入
- 1 先在NuGet中添加相关依赖项
- 2 定义实体类
- 3 添加Context类
- 4 获取数据库连接字符串
- 5 修改配置文件APP.config
- 三、验证测试
- 1 界面窗口布局
- 2 按钮事件
- 3 效果
本文所介绍的是真正的EF+CodeFirst,不需要提前建表,由代码自动生成!
一、提前准备
1 下载Sqlite Expert
下载Sqlite Expert
下载免费版个人版就够用了
- 下载后新建数据库,然后保存到你指定的目录即可(保存为.db文件)
2 安装VS扩展工具
- 然后在VS中->工具->扩展和更新->联机
- 搜索SQLite/SQL Server Compact ToolBox ,下载完后重启VS
- 在同一个地方再搜索dotConnect ADO.NET Data Provider for SQLite,记得不是安装Standard的,点下载后会出现个弹出框下载,下载完后安装时要先关闭VS
二、项目接入
1 先在NuGet中添加相关依赖项
- SQLite和SQLite.CodeFirst
2 定义实体类
public class Student
{public int ID { get; set; }public string Name { get; set; }public int Age { get; set; }public string Gender { get; set; }
}
3 添加Context类
- 继承自DbContext以及Model配置类
public class StudentContext : DbContext
{//定义属性,便于外部访问数据表public DbSet<Student> Students { get { return Set<Student>(); } }public StudentContext() : base("dbConn"){}protected override void OnModelCreating(DbModelBuilder modelBuilder){ModelConfiguration.Configure(modelBuilder);var init = new SqliteDropCreateDatabaseWhenModelChanges<StudentContext>(modelBuilder);//var init = new SqliteCreateDatabaseIfNotExists<MyBookDB>(modelBuilder);Database.SetInitializer(init);}
}public class ModelConfiguration
{public static void Configure(DbModelBuilder modelBuilder){ConfigureBookEntity(modelBuilder);}private static void ConfigureBookEntity(DbModelBuilder modelBuilder){modelBuilder.Entity<Student>();}
}
4 获取数据库连接字符串
- 工具->连接到数据库->更改->选择SQLite Database->Browse数据库文件->测试成功后复制Connection String
5 修改配置文件APP.config
- 在</system.data>结点后面添加连接字符串
<connectionStrings><add name="SQLiteConnect" connectionString="Data Source=F:\xxx\SQLLiteTest.db" providerName="System.Data.SQLite.EF6"/></connectionStrings>
- 添加Provider结点(主要原因是SQLite的invarianName不是在自动生成的.EF6里面)
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
三、验证测试
1 界面窗口布局
2 按钮事件
private void btnAdd_Click(object sender, EventArgs e)
{List<Student> books = new List<Student>(){new Student() {ID=1, Name = "小明", Age = 11, Gender = "男" },new Student() {ID=2, Name = "老王", Age = 12, Gender = "男" },new Student() {ID=3, Name = "小郭", Age = 10, Gender = "女" },new Student() {ID=4, Name = "小李", Age = 11, Gender = "男" },new Student() {ID=5, Name = "小玉", Age = 9, Gender = "女" },};using (var db = new StudentContext()){db.Students.AddRange(books);int count = db.SaveChanges();this.Text = $"{DateTime.Now}, 插入{count}条记录";}
}
private void btnModify_Click(object sender, EventArgs e)
{using (var db = new StudentContext()){var students = db.Students.FirstOrDefault(x => x.Name == "小李");if (students != null){students.Age += 1;int count = db.SaveChanges();this.Text = $"{DateTime.Now}, 修改{count}条记录";}}
}private void btnDel_Click(object sender, EventArgs e)
{using (var db = new StudentContext()){var students = db.Students.FirstOrDefault(x => x.Name == "小玉");if (students != null){var result = db.Students.Remove(students);int count = db.SaveChanges();this.Text = $"{DateTime.Now}, 删除{count}条记录";}}
}private void btnQuery_Click(object sender, EventArgs e)
{using (var db = new StudentContext()){var students = db.Students.Where(x => x.Name == "老王").OrderByDescending(x => x.Age).ToList();this.Text = $"{DateTime.Now}, 查到{students.Count}条记录";this.dataGridView1.DataSource = students;}
}private void btnRefresh_Click(object sender, EventArgs e)
{using (var db = new StudentContext()){var students = db.Students.ToList();this.dataGridView1.DataSource = students;}
}
3 效果
- 根据代码生成表
这篇关于『SQLite』C#/.NET EF+SQLite.CodeFirst——真正实现CodeFirst的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!