排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
分类:
Free sql
前言
freesql 基础(增删改查)
先添加依赖
private readonly ILogger<HomeController> _logger; private readonly IFreeSql _FreeSql = null; public HomeController(ILogger<HomeController> logger, IStudent student, IFreeSql FreeSql) { _logger = logger; _FreeSql = FreeSql; }
增加
//增加 Student student = new Student(); //查询最大ID student.SId = (Convert.ToInt32(_FreeSql.Select<Student>().Max(a => a.SId)) + 1).ToString(); student.Sname = "王五"; student.Sage = DateTime.Now.AddYears(-18); student.Ssex = "男"; var repo = _FreeSql.GetRepository<Student>(); //本次使用的是单个添加,批量添加将 student 替换成List<Student> repo.Insert(student);
删除
//删除 //出于安全考虑,没有条件不执行删除动作,避免误删除全表数据。删除全表数据:fsql.Delete<T>().Where("1=1").ExecuteAffrows() //返回受影响行数 long delcount = _FreeSql.Select<Student>().Where(a => a.Sname == "李四").ToDelete().ExecuteAffrows(); //支持删除的4种方式 //主键集合 new[] { 主键值1, 主键值2 } //单个对象 //对象集合 new[] { Student对象1, Student对象2 } //匿名对象 new { id = 1 } //主键集合 new[] { 主键值1, 主键值2 } //主键包含在集合内的数据将会被删除 //等价于DELETE FROM Student WHERE (SId = '1' OR SId = '2') var t1 = _FreeSql.Delete<Student>(new[] { "1", "2" }).ToSql(); //单个对象 //等价于DELETE FROM Student WHERE (SId = '1') var t2 = _FreeSql.Delete<Student>(new Student{ SId = "1", Sname = "王五" }).ToSql(); //对象集合 //等价于DELETE FROM Student WHERE (SId = '1' OR `SId ` = '2') var t3 = _FreeSql.Delete<Student>(new[] { new Student{ SId = "1", Sname = "王五" }, new Student{ SId = "2", Sname = "李四" } }).ToSql(); //匿名对象 //等价于DELETE FROM Student WHERE (SId = '1') var t4 = _FreeSql.Delete<Student>(new { SId = '1'}).ToSql();
修改
///修改 ///方法里传主键值、主键值集合、实体、实体集合、匿名对象、匿名对象集合 /// _FreeSql.Update<Student>(new[] {"1"}) /// 或 ///_FreeSql.Update<Student>(new {"1"}) ///_FreeSql.Update<Student>("1") long count = _FreeSql.Update<Student>("1") .Set(a => new Student { Sname = "李四", Ssex = "女" }) .ExecuteAffrows(); //set可拼接多个 long count = _FreeSql.Update<Student>("1") .Set(a => a.Sname == "李四") .Set(a => a.Ssex == "女") .ExecuteAffrows();
简单查询(查询还有很多种,这里测试了一种最简单的)
//查询 List<Student> dt = _FreeSql.Select<Student>().ToList(); //查询 List<Student> dt1 = _FreeSql.Select<Student>().Where(a => a.Ssex == "女").ToList(); //查询
更多信息在freesql官方文档
传送门指南 | FreeSql 官方文档
评价