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();
(一)入門函數