ef core 连接sqlite

引用

Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Design

使用Navicat创建sqlite数据库

创建上下文

public class RegionTreeDbContext : DbContext
{
	private string connectionString;
	public DbSet<RegionTree> Regions { get; set; }

	public RegionTreeDbContext(string connectionString)
	{
		this.connectionString = connectionString;
	}

	protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
	{
		optionsBuilder.UseSqlite(connectionString);
	}
}

查询数据

private static string connectionString = @"Data Source=your path\your db.db";
public void Query()
{
	//ef core sqlite 使用
	using(RegionTreeDbContext db=new RegionTreeDbContext(connectionString))
	{
		var region = db.Regions.FirstOrDefault();
	}
}

示例代码

QueryTree

参考资料

.Net Core EF Core之Sqlite使用及部署

posted @ 2020-04-12 20:16  Lulus  阅读(3745)  评论(0编辑  收藏  举报