Asp.Net 高性能ORM框架——SqlSugar
公司团队项目、产品已经完全抛弃EF,SqlSugar定位不是ORM,而是为了方便的让你去写Sql。
SqlSugar 媲美原生ADO.NET的性能,语法简洁,并且支持 Json 、Dynamic、 List<T>、 List<string[]>、 ValueType和 Dictionary 等多种类型的返回值,有很多亮点。
SqlSugar 功能介绍:
查询/Search
1、Queryable 用于拉姆达表达式操作(不久将支持Join多表) / Select single table or view
//---------Queryable<T>, 扩展函数查询 (针对单表或者视图查询)-----//
//查询条数
var count = db.Queryable<Student>().Where(c => c.id > 10).Count();
//查询第一条
var first = db.Queryable<Student>().Where(c => c.id == ).First();
var first2 = db.Queryable<Student>().Where(c => c.id == ).FirstOrDefault(); //取10-20条
var page1 = db.Queryable<Student>().Where(c => c.id > ).OrderBy(it => it.id).Skip().Take().ToList(); //上一句的简化写法,同样取10-20条
var page2 = db.Queryable<Student>().Where(c => c.id > ).OrderBy(it => it.id).ToPageList(, );
//从第2条开始以后取所有
var skip = db.Queryable<Student>().Where(c => c.id > ).OrderBy(it => it.id).Skip().ToList();
//查询所有
var student = db.Queryable<Student>().ToList();
var studentDynamic = db.Queryable<Student>().ToDynamic();
var studentJson = db.Queryable<Student>().ToJson(); //查询单条
var single = db.Queryable<Student>().Single(c => c.id == 1);
//查询单条没有记录返回空对象
var single2 = db.Queryable<Student>().Where(c => c.id == 1).SingleOrDefault();
//取前2条
var take = db.Queryable<Student>().Where(c => c.id > ).OrderBy(it => it.id).Take().ToList(); // Not like
string conval = "a";
var notLike = db.Queryable<Student>().Where(c => !c.name.Contains(conval.ToString())).ToList();
//Like
conval = "三";
var like = db.Queryable<Student>().Where(c => c.name.Contains(conval)).ToList(); // 可以在拉姆达使用 ToString和 Convert,比EF出色的地方
var convert1 = db.Queryable<Student>().Where(c => c.name == "a".ToString()).ToList();
var convert2 = db.Queryable<Student>().Where(c => c.id == Convert.ToInt32("")).ToList();//
var convert3 = db.Queryable<Student>().Where(c => DateTime.Now > Convert.ToDateTime("2015-1-1")).ToList();
var convert4 = db.Queryable<Student>().Where(c => DateTime.Now > DateTime.Now).ToList(); //支持字符串Where 让你解决,更复杂的查询
var student12 = db.Queryable<Student>().Where(c => "a" == "a").Where("id>100").ToList();
var student13 = db.Queryable<Student>().Where(c => "a" == "a").Where("id>100 and id in( select 1)").ToList(); //存在记录反回true,则否返回false
bool isAny100 = db.Queryable<Student>().Any(c => c.id == );
bool isAny1 = db.Queryable<Student>().Any(c => c.id == ); //获取最大Id
object maxId = db.Queryable<Student>().Max(it => it.id);
int maxId1 = db.Queryable<Student>().Max(it => it.id).ObjToInt();//拉姆达
int maxId2 = db.Queryable<Student>().Max<Student, int>("id"); //字符串写法 //获取最小
int minId1 = db.Queryable<Student>().Where(c => c.id > ).Min(it => it.id).ObjToInt();//拉姆达
int minId2 = db.Queryable<Student>().Where(c => c.id > ).Min<Student, int>("id");//字符串写法 //order By
var orderList = db.Queryable<Student>().OrderBy("id desc,name asc").ToList();//字符串支持多个排序
//可以多个order by表达示
var order2List = db.Queryable<Student>().OrderBy(it=>it.name).OrderBy(it => it.id, OrderByType.desc).ToList(); // order by name as ,order by id desc //In
var intArray=new []{"", "", ""};
var intList = intArray.ToList();
var list0 = db.Queryable<Student>().In(it => it.id, ,,).ToList();
var list1 = db.Queryable<Student>().In(it=>it.id, intArray).ToList();
var list2 = db.Queryable<Student>().In("id", intArray).ToList();
var list3 = db.Queryable<Student>().In(it => it.id, intList).ToList();
var list4 = db.Queryable<Student>().In("id", intList).ToList(); //分组查询
var list7 = db.Queryable<Student>().Where(c => c.id < ).GroupBy(it => it.sex).Select("sex,Count=count(*)").ToDynamic();
var list8 = db.Queryable<Student>().Where(c => c.id < ).GroupBy(it => it.sex).GroupBy(it=>it.id).Select("id,sex,Count=count(*)").ToDynamic();
List<SexTotal> list9 = db.Queryable<Student>().Where(c => c.id < ).GroupBy(it => it.sex).Select<Student, SexTotal>("Sex,Count=count(*)").ToList();
List<SexTotal> list10 = db.Queryable<Student>().Where(c => c.id < ).GroupBy("sex").Select<Student, SexTotal>("Sex,Count=count(*)").ToList();
//SELECT Sex,Count=count(*) FROM Student WHERE 1=1 AND (id < 20) GROUP BY Sex --生成结果
2、SqlQuery 用于执行原生SQL和存储过程
//转成list
List<Student> list1 = db.SqlQuery<Student>("select * from Student");
//转成list带参
List<Student> list2 = db.SqlQuery<Student>("select * from Student where id=@id", new { id = });
//转成dynamic
dynamic list3 = db.SqlQueryDynamic("select * from student");
//转成json
string list4 = db.SqlQueryJson("select * from student");
//返回int
var list5 = db.SqlQuery<int>("select top 1 id from Student").SingleOrDefault();
//反回键值
Dictionary<string, string> list6 = db.SqlQuery<KeyValuePair<string, string>>("select id,name from Student").ToDictionary(it => it.Key, it => it.Value);
//反回List<string[]>
var list7 = db.SqlQuery<string[]>("select top 1 id,name from Student").SingleOrDefault();
//存储过程
var spResult = db.SqlQuery<School>("exec sp_school @p1,@p2", new { p1 = , p2 = }); //获取第一行第一列的值
string v1 = db.GetString("select '张三' as name");
int v2 = db.GetInt("select 1 as name");
double v3 = db.GetDouble("select 1 as name");
decimal v4 = db.GetDecimal("select 1 as name");
3、Sqlable 更贴近SQL的一种编程方式,适用于复杂查询
//---------Sqlable,创建多表查询---------// //多表查询
List<School> dataList = db.Sqlable()
.From("school", "s")
.Join("student", "st", "st.id", "s.id", JoinType.INNER)
.Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
.Where("s.id>100 and s.id<@id")
.Where("1=1")//可以多个WHERE
.SelectToList<School/*新的Model我这里没有所以写的School*/>("st.*", new { id = }); //多表分页
List<School> dataPageList = db.Sqlable()
.From("school", "s")
.Join("student", "st", "st.id", "s.id", JoinType.INNER)
.Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
.Where("s.id>100 and s.id<100")
.SelectToPageList<School>("st.*", "s.id", , ); //多表分页WHERE加子查询
List<School> dataPageList2 = db.Sqlable()
.From("school", "s")
.Join("student", "st", "st.id", "s.id", JoinType.INNER)
.Join("student", "st2", "st2.id", "st.id", JoinType.LEFT)
.Where("s.id>100 and s.id<100 and s.id in (select 1 )" /*这里面写子查询都可以*/)
.SelectToPageList<School>("st.*", "s.id", , ); //--------转成List Dynmaic 或者 Json-----// //不分页
var list1 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDynamic("*", new { id = });
var list2 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToJson("*", new { id = });
var list3 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToDataTable("*", new { id = }); //分页
var list4 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", , , new { id = });
var list5 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageTable("s.*", "l.id", , , new { id = });
var list6 = db.Sqlable().From("student", "s").Join("school", "l", "s.sch_id", "l.id and l.id=@id", JoinType.INNER).SelectToPageDynamic("s.*", "l.id", , , new { id = }); //--------拼接-----//
Sqlable sable = db.Sqlable().From<Student>("s").Join<School>("l", "s.sch_id", "l.id", JoinType.INNER);
string name = "a";
int id = ;
if (!string.IsNullOrEmpty(name))
{
sable = sable.Where("s.name=@name");
}
if (!string.IsNullOrEmpty(name))
{
sable = sable.Where("s.id=@id or s.id=100");
}
if (id > )
{
sable = sable.Where("l.id in (select top 10 id from school)");//where加子查询
}
var pars = new { id = id, name = name };
int pageCount = sable.Count(pars);
var list7 = sable.SelectToPageList<Student>("s.*","l.id desc",,, pars);
4、新容器转换
using (SqlSugarClient db = SugarDao.GetInstance())
{
var list2 = db.Queryable<Student>().Where(c => c.id < ).Select(c => new classNew { newid = c.id, newname = c.name, xx_name = c.name }).ToList();//不支持匿名类转换,也不建议使用 var list3 = db.Queryable<Student>().Where(c => c.id < ).Select(c => new { newid = c.id, newname = c.name, xx_name = c.name }).ToDynamic();//匿名类转换 var list4 = db.Queryable<Student>().Where(c => c.id < ).Select("id as newid, name as newname ,name as xx_name").ToDynamic();//匿名类转换
添加/Add
using (SqlSugarClient db = SugarDao.GetInstance())//开启数据库连接
{
Student s = new Student()
{
name = "张" + new Random().Next(, int.MaxValue)
}; db.Insert(s); //插入一条记录 (有主键也好,没主键也好,有自增列也好都可以插进去) List<Student> list = new List<Student>()
{
new Student()
{
name="张"+new Random().Next(,int.MaxValue)
},
new Student()
{
name="张"+new Random().Next(,int.MaxValue)
}
}; db.InsertRange(list); //批量插入
}
}
更新/Update
using (var db = SugarDao.GetInstance())
{
//指定列更新
db.Update<School>(new { name = "蓝翔2" }, it => it.id == id);
db.Update<School, int>(new { name = "蓝翔2" }, , , );
db.Update<School, string>(new { name = "蓝翔2" }, new string[] { "", "" }); //整个实体更新
db.Update(new School { id = id, name = "蓝翔2" });
db.Update<School>(new School { id = id, name = "蓝翔2" }, it => it.id == id); //设置不更新列
db.DisableUpdateColumns = new string[] { "CreateTime" };//设置CreateTime不更新 TestUpdateColumns updObj = new TestUpdateColumns()
{
VGUID = Guid.Parse("542b5a27-6984-47c7-a8ee-359e483c8470"),
Name = "xx",
Name2 = "xx2",
IdentityField = ,
CreateTime = null
}; //CreateTime将不会被更新
db.Update(updObj);
//以前实现这种更新需要用指定列的方式实现,现在就简单多了。 }
删除和假删除
using (var db = SugarDao.GetInstance())
{
//真删除
db.Delete<School, int>();//注意主键必需为实体类的第一个属性
db.Delete<School>(it => it.id > );
db.Delete<School, string>(new string[] { "", "", "" }); //假删除
//db.FalseDelete<school>("is_del", 100);
//等同于 update school set is_del=0 where id in(100)
//db.FalseDelete<school>("is_del", it=>it.id==100);
}
还有更多的功能介绍没有复制过来……
Asp.Net 高性能ORM框架——SqlSugar的更多相关文章
- Asp.Net 高性能ORM框架 SqlSugar.ORM 2.8
3.0最新API: http://www.cnblogs.com/sunkaixuan/p/5911334.html 1.前言/Preface SqlSugar从去年到现在已经一年了,版本从1.0升到 ...
- SqlSugar Asp.Net 高性能ORM框架
SqlSugar从去年到现在已经一年了,版本从1.0升到了现在的2.4.1 ,这是一个稳定版本 ,有数家公司已经项目上线,在这里我将SqlSugar的功能重新整理成一篇新的贴子,希望大家喜欢. 公司团 ...
- .Net开源SqlServer ORM框架SqlSugar整理
一.链接整理 官方Git源代码地址: https://github.com/sunkaixuan/SqlSugar 最新发布版更新地址:当前版本Release 3.5.2.1 https://gith ...
- 高性能ORM 框架之 MySqlSugar
mysql 3.X API地址: http://www.cnblogs.com/sunkaixuan/p/5987308.html MySqlSugar 1.5 API 一.介简 SqlSugar ...
- ASP.NET--常用ORM框架
subsonic massive dapper 性能不错,接近原声的ADO.NET 这个是大家推荐的,stackoverflow用的就是这个框架 petapoco 这些都是ORM框架
- .NET 开源SqlServer ORM框架 SqlSugar 3.0 API
3.1.x ,将作为3.X系统的最后一个版本,下面将会开发 全新的功能 更新列表:https://github.com/sunkaixuan/SqlSugar/releases 优点: SqlSuga ...
- 高性能ORM框架XLinq功能详细介绍
之前简单介绍了XLinq的一些功能,有很多功能都没有提到,现在给XLinq加了一些功能,这次把所有功能都介绍一遍. 设计目标 易用性 在使用一个框架的时候 应该没几个人会喜欢写一大堆的配置文件吧 也应 ...
- 轻量级高性能ORM框架:Dapper高级玩法
Dapper高级玩法1: 数据库中带下划线的表字段自动匹配无下划线的Model字段. Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true; 备 ...
- 目前的.NET(C#)世界里,主流的ORM框架
推荐一些常用的asp.net ORM框架 SqlSugar (国内) Dos.ORM (国内) Chloe (国内) StackExchange/Dapper (国外) Entity Framewor ...
随机推荐
- 删除github上个人的repositories的操作步骤
- 《网络对抗》——逆向及Bof基础实践
<网络对抗>--逆向及Bof基础实践 原理 利用foo函数的Bof漏洞,构造一个攻击输入字符串,覆盖返回地址,触发getShell函数. 手工修改可执行文件,改变程序执行流程,直接跳转到g ...
- 不明原因报错集中处理:Undefined
1, NSGenericException错误 Terminating app due to uncaught exception 'NSGenericException', reason: '*** ...
- git2
1,开源的代码管理工具 2,分布式管理工具(更安全,可以脱网操作) 3,git的分支管理更加便捷. 4,代码的传输更新速度更快 利用git可以进入多人配合代码开发.有备份.协同 sudo apt-ge ...
- Metasploit应用举例
本篇文章包含以下几方面内容: 1.Metasploit端口扫描: 2.用其他模块 3.metasploit smb获取系统信息 4.Metsploit服务识别 5.ftp识别: 6.metasploi ...
- Git 基础 —— 安装 配置 别名 对象
Git 基础学习系列 Git 基础 -- 安装 配置 别名 对象 Git 基础 -- 常用命令 Git 基础 -- 常见使用场景 Git基础 -- Github 的使用 Git 安装 Git下载地址 ...
- 织梦dedecms多选checkbox字段值独立输出样式调用方法
在做织梦网站时发现dedecms的自建字段的checkboxcook输出是一段证不能把每个标签分开写样式,在晚上查了好多没页没有好的,最终用中方式来做会好 {dede:field.fyuantese ...
- ovs-ofctl: s1 is not a bridge or a socket 解决方法
参考: ovs-vsctl: Error detected while setting up bridge ovs-ofctl: s1 is not a bridge or a socket 解决方法 ...
- 树莓派GPIO
- UVa 820 因特网带宽(最大流)
https://vjudge.net/problem/UVA-820 题意: 给出所有计算机之间的路径和路径容量后求出两个给定结点之间的流通总容量. 思路: 裸的最大流问题.注意有个比较坑的地方,最后 ...