【NET】【SQLite】【EF】在Net中以CodeFirst模式使用SQLite

发布时间 2023-07-17 20:47:54作者: 神羽鸦青

在Net程序中使用SQLite,以CodeFirst模式,使用EF,不手工拼接查询代码的方式。

需要改动三个部分,分别是安装Nuget包添加配置编写代码

安装Nuget包

这部分比较简单,直接Nuget包中下载即可

  1. System.Data.SQLite
  2. System.Data.SQLite.EF6
  3. System.Data.SQLite.LINQ
  4. SQLite.CodeFirst

这些包会需要几个依赖,比如EntityFramework,不过不必担心,Nuget会自行下载。

添加配置

Nuget包管理器会在安装时自动生成一些配置,不过这些配置有时会出现问题,可以在出问题时检查entityFramework节点下的providerssystem.data下的DbProviderFactories节点,是否正确。

<configuration>
	<entityFramework>
        <!--这个节点通常需要以下两个provider-->
    	<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.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" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
      <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" />
        </DbProviderFactories>
	</system.data>
</configuration>

添加配置部分最重要的是添加数据库链接到配置中,在根节点的connectionString中,如果没有该节点直接写一个即可

<configuration>
	<connectionStrings>
        <!--name属性会在代码中用到,connectionString的source可以使用绝对/相对路径,或者在调试使用|DataDirectory|时指定到Debug/Release文件夹下-->
    	<add name="SqliteContext" connectionString="data source=.\db" providerName="System.Data.SQLite.EF6" />
    </connectionStrings>
</configuration>
编写代码
  1. Dbcontext
using SQLite.CodeFirst;
using System.Data.Entity;

public class SqliteContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // 以代码优先的方式运行,如果数据库不存在会创建
            var connection = new SqliteCreateDatabaseIfNotExists<SqliteContext>(modelBuilder);
            Database.SetInitializer(connection);
        }
    	// 这是构造函数
        public SqliteContext()
            : base("SqliteContext") // 这里的名字要与配置中的connectionString的add节点的name保持一致
        {
        }
    	// 这里是对实体类的映射
        public DbSet<user> users { get; set; }
    }
  1. 实体类
public class user
{
    [System.ComponentModel.DataAnnotations.Key]// 主键
    [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(DatabaseGeneratedOption.Identity)]// 自增
	public int Id{get;set;} 
    public string Name{get;set;}
}
  1. 然后就可以用lambda的方式写查询了
public class Sqlitehelp
{
    public static void update(int id)
    {
        using (EfContext db = new EfContext())
        {
            user old = db.users.Where(r=>r.Id==id).FirstOrDefault()
            old.Name = "神羽鸦青";
            db.SaveChanges();
        }
    }
}

使用这种方式可以不用在程序中拼接SQL查询语句,也不用实现创建sqlite数据库,用起来方便多了。