這是針對dapper的一個擴展,支持lambda表達式的寫法,鏈式風格讓開發者使用起來更加優雅、直觀。如今暫時只有MsSql的擴展,也沒有實現事務的寫法,將會在後續的版本補充。html
這是我的業餘的開源小項目,若是你們有更好的實現方式和好的建議歡迎拍磚git
本項目已經在github上開源了:Sikiro.DapperLambdaExtension.MsSqlgithub
去年寫了《整理本身的.net工具庫》,裏面提供的源碼從新發布到了github並用新的項目名Sikiro.Tookitssql
這兩個項目都發布到Nuget上了,能夠在Nuget搜索Sikiro能夠所有查看到c#
另外該項目會用到一些表達式樹的知識,若是有興趣的朋友能夠先去了解,我以前也寫過一篇簡單的文章《表達式樹的解析.》app
下面是簡單的使用介紹工具
你能夠運行如下下命令在你的項目中安裝 Sikiro.DapperLambdaExtension.MsSql。ui
PM> Install-Package Sikiro.DapperLambdaExtension.MsSql
var con = new SqlConnection("Data Source=192.168.13.46;Initial Catalog=SkyChen;Persist Security Info=True;User ID=sa;Password=123456789");
[Table("SYS_USER")] public class SysUser { /// <summary> /// 主鍵 /// </summary> [Key] [Required] [StringLength(32)] [Display(Name = "主鍵")] [Column("SYS_USERID")] public string SysUserid { get; set; } /// <summary> /// 建立時間 /// </summary> [Required] [Display(Name = "建立時間")] [Column("CREATE_DATETIME")] public DateTime CreateDatetime { get; set; } /// <summary> /// 郵箱 /// </summary> [Required] [StringLength(32)] [Display(Name = "郵箱")] [Column("EMAIL")] public string Email { get; set; } /// <summary> /// USER_STATUS /// </summary> [Required] [Display(Name = "USER_STATUS")] [Column("USER_STATUS")] public int UserStatus { get; set; } }
con.CommandSet<SysUser>().Insert(new SysUser { CreateDatetime = DateTime.Now, Email = "287245177@qq.com", SysUserid = Guid.NewGuid().ToString("N"), UserName = "chengong", });
當不存在某條件記錄Insert.net
con.CommandSet<SysUser>().IfNotExists(a => a.Email == "287245177@qq.com").Insert(new SysUser { CreateDatetime = DateTime.Now, Email = "287245177@qq.com", SysUserid = Guid.NewGuid().ToString("N"), UserName = "chengong", });
您能夠根據某個條件把指定字段更新code
con.CommandSet<SysUser>().Where(a => a.Email == "287245177@qq.com").Update(a => new SysUser { Email = "123456789@qq.com" });
也能夠根據主鍵來更新整個實體字段信息
User.Email = "123456789@qq.com"; condb.CommandSet<SysUser>().Update(User);
您能夠根據條件來刪除數據
con.CommandSet<SysUser>().Where(a => a.Email == "287245177@qq.com").Delete()
獲取過濾條件的一條數據(第一條)
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").Get()
固然咱們也能夠查詢出符合條件的數據集
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").OrderBy(b => b.Email).Top(10).Select(a => a.Email).ToList();
還有分頁
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com") .OrderBy(a => a.CreateDatetime) .Select(a => new SysUser { Email = a.Email, CreateDatetime = a.CreateDatetime, SysUserid = a.SysUserid }) .PageList(1, 10);
先更新再把結果查詢出來
con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com") .OrderBy(a => a.CreateDatetime) .Select(a => new SysUser { Email = a.Email }) .UpdateSelect(a => new SysUser { Email = "2530665632@qq.com" });
con.Transaction(tc => { var sysUserid = tc.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").Select(a => a.SysUserid).Get(); tc.CommandSet<SysUser>().Where(a => a.SysUserid == sysUserid).Delete(); tc.CommandSet<SysUser>().Insert(new SysUser { CreateDatetime = DateTime.Now, Email = "287245177@qq.com", Mobile = "13536059332", RealName = "大笨貞", SysUserid = Guid.NewGuid().ToString("N"), UserName = "fengshuzhen", UserStatus = 1, UserType = 1, Password = "asdasdad" }); });
using (var con = new SqlConnection("Data Source=192.168.13.46;Initial Catalog=SkyChen;Persist Security Info=True;User ID=sa;Password=123456789")) { con.CommandSet<SysUser>().Insert(new SysUser { CreateDatetime = DateTime.Now, Email = "287245177@qq.com", SysUserid = Guid.NewGuid().ToString("N"), UserName = "chengong", }); var model = con.QuerySet<SysUser>().Where(a => a.Email == "287245177@qq.com").Get(); con.CommandSet<SysUser>().Where(a => a.SysUserid == model.SysUserid) .Update(a => new SysUser { Email = "2548987@qq.com" }); con.CommandSet<SysUser>().Where(a => a.SysUserid == model.SysUserid).Delete(); }
除了簡單的CURD還有Count、Sum、Exists
第一個版本有未完善的地方,若是你們有很好的建議歡迎隨時向我提,但願獲得你們的建議後能良好的改善升級