FreeSql 是一个功能强大的对象关系映射程序(O/RM),支持 .NETCore 2.1+ 或 .NETFramework 4.5+(QQ群:4336577)html
FreeSql采用MIT开源协议托管于 github。git
使用模型执行数据访问,模型由实体类表示数据库表或视图,用于查询和保存数据。github
可从现有数据库生成实体模型,FreeSql 提供 IDbFirst 接口实现生生成实体模型。sql
或者手动建立模型,基于模型建立或修改数据库,提供 ICodeFirst 同步结构的 API(甚至能够作到开发阶段自动同步)。数据库
using FreeSql.DataAnnotations; using System; public class Blog { [Column(IsIdentity = true, IsPrimary = true)] public int BlogId { get; set; } public string Url { get; set; } public int Rating { get; set; } }
dotnet add packages FreeSql.Provider.Sqlite数组
var connstr = @"Data Source=|DataDirectory|\db1.db;Attachs=db2.db;Pooling=true;Max Pool Size=10"; IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, connstr) .UseAutoSyncStructure(true) //自动同步实体结构到数据库 .Build();
注意: IFreeSql 在项目中应以单例声明,而不是在每次使用的时候建立。异步
程序运行中FreeSql会检查AutoSyncStructure参数,以此条件判断是否对比实体与数据库结构之间的变化,达到自动迁移的目的。ide
var blogs = fsql.Select<Blog>() .Where(b => b.Rating > 3) .OrderBy(b => b.Url) .Skip(100) .Limit(10) //第100行-110行的记录 .ToList();
var blog = new Blog { Url = "http://sample.com" }; blog.BlogId = (int)fsql.Insert<Blog>() .AppendData(blog) .ExecuteIdentity();
fsql.Update<Blog>() .Set(b => b.Url, "http://sample2222.com") .Where(b => b.Url == "http://sample.com") .ExecuteAffrows();
fsql.Delete<Blog>() .Where(b => b.Url == "http://sample.com") .ExecuteAffrows();
(一)入门函数