Sqlite For C# EF
Sqlite EF
1.创建控制台项目Lession1
2.安装Sqlite
install-package system.data.sqlite
install-package sqlite.Codefirst //Code First 用于生成数据库表结构,否则不会自动生成表结构
3.修改配置文件 app.config
<?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>
4.建立模型
using System.ComponentModel.DataAnnotations.Schema;
namespace Lesson1
{
[Table("StudentInfo")]
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
}
5.建立数据上下文
using SQLite.CodeFirst;
using System.Data.Entity;
namespace Lesson1
{
public class MyContext:DbContext
{
//指定自定义的数据库连接 MyConnection
public MyContext() : base("MyConnection") { }
//重写如下方法,否则默认用的是SqlServer 的模型创建方法
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var init = new SqliteCreateDatabaseIfNotExists<MyContext>(modelBuilder);
Database.SetInitializer(init);
}
public DbSet<Student> Students { get; set; }
}
}
6.编写测试用例
using System;
using System.Collections.Generic;
using System.Linq;
namespace Lesson1
{
internal class Program
{
static void Main(string[] args)
{
Student s1=new Student() { Id=1,Name="test1"};
Student s2 = new Student() { Id = 2, Name = "test2" };
List<Student> students = new List<Student>() { new Student { Id=3,Name="test3",
},new Student{ Id=4,Name="test4"} };
using(var mydb=new MyContext())
{
mydb.Students.AddRange(students);
mydb.SaveChanges();
var query = from r in mydb.Students select r;
foreach (var item in query) {
Console.WriteLine($"编号:{item.Id},姓名:{item.Name}");
}
Console.ReadLine();
}
}
}
}