SqlSugar删除数据
1、根据实体删除
1.1 强类型实体
需要配置主键 ,根据主键删除需要给实体配置主键,参考文档实体配置
//单个实体 db.Deleteable<Student>( new Student() { Id = 1 }).ExecuteCommand(); //List<实体> (可以不加Where) List<Student> list= new List<Student>(){ new Student() { Id = 1 } }; db.Deleteable<Student>(list).ExecuteCommand(); //批量删除 //批量删除+分页 db.Deleteable<Order>(list).PageSize(500).ExecuteCommand(); |
函数 | 说明 |
ExecuteCommand | 返回受影响行数 , update where 如果没找到那么就会返回 0 |
ExecuteCommandHasChange | 返回bool ,等同于 bool isChange= ExecuteCommand()>0 |
2.2无主键实体删除
注意:请升级到5.1.4.60-preview02
List<T> list= new List<T>(){...}; db.Deleteable<Order>().WhereColumns(list,it=> new { it.Id}).ExecuteCommand(); |
2.3 Object、接口、抽象类 删除
//2.Object对象插入 (5.1.3.45) //o必须是真实的类对象 (比如根据type反射出来的对象,或者转成了Object的实体) db.DeleteableByObject(o).ExecuteCommand(); //更多功能 :动态建类等 https: //www.donet5.com/Home/Doc?typeId=2562 |
2、根据主键
db.Deleteable<Student>().In(1).ExecuteCommand(); //无主键用法 db.Deleteable<Order>().In(it=>it.Id,1).ExecuteCommand(); |
3、根据主键数组
db.Deleteable<Student>().In( new int [] { 1, 2 }).ExecuteCommand(); //无主键用法 db.Deleteable<Student>().In(it=>it.Id, new int [] { 1, 2 }).ExecuteCommand(); //Oracle用户注意:这种方式只能删除1000,用达式方式.Where(it=>ids.Contains(it.Id)支持1000以上 |
4、根据表达式
db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand(); |
5、联表删除
db.Deleteable<Student>() .Where(p => SqlFunc.Subqueryable<School>().Where(s => s.Id == p.SchoolId).Any()) .ExecuteCommand() //.Where(s => s.Id == p.SchoolId) s和p的关联条件不能少,不然就全删了 |
6、无实体删除
db.Deleteable< object >().AS( "[Order]" ).Where( "id=@id" , new { id=1}).ExecuteCommand(); db.Deleteable< object >().AS( "[Order]" ).Where( "id in (@id) " , new { id= new int []{1,2,3}}).ExecuteCommand(); //批量 //根据字典集合删除 List<Dictionary< string , object >> list= new List<Dictionary< string , object >>; list.Add(字典); db.Deleteable< object >().AS( "[Order]" ).WhereColumns(list).ExecuteCommand(); //DataTable也可以转成字典集合 List<Dictionary< string , object >> list= db.Utilities.DataTableToDictionaryList(dataTable); //转成字典 |
7、全局过滤器(5.0.4.1)
//配置表过滤器 db.QueryFilter.Add( new TableFilterItem<Order>(it => it.Name.Contains( "a" ))); //查询有效 db.Queryable<Order>().ToList(); //SELECT [Id],[Name],[Price],[CreateTime],[CustomId] FROM [Order] WHERE ([Name] like '%'+@MethodConst0+'%') //删除也有效 db.Deleteable<Order>().EnableQueryFilter().Where(it=>it.Id==1).ExecuteCommand(); //DELETE FROM [Order] WHERE ([Name] like '%'+@MethodConst1000+'%') AND ( [Id] = @Id0 ) |
8、逻辑删除(5.0.4.3)
逻辑删除方式1:
优点:简单直接用,如果有数据过滤器建议用方式2
实体属性有isdelete或者isdeleted
db.Deleteable<LogicTest>().In(100).IsLogic().ExecuteCommand(); //假删除 软删除 |
指定属性
db.Deleteable<LogicTest>().In(100).IsLogic().ExecuteCommand( "mydelete" ); |
指定属性并且修改时间
db.Deleteable<LogicTest>().In(100).IsLogic().ExecuteCommand( "mydelete" , true ,DateTime.Now, "UpdateTime" ); |
指定属性并且修改时间+用户
db.Deleteable<LogicTest>().In(100) .IsLogic() .ExecuteCommand( "mydelete" , true , DateTime.Now, "UpdateTime" , "ModifierName" ,userId); |
说明:用法与删除一样唯一多了个.IsLogic()
逻辑删除方式2:
优点:可以结合更新过滤器一起生效
public void FalseDelete<T>(Expression<Func<T, bool >> exp) where T: class ,IDeleted, new () //约束3个不能少 { db.Updateable<T>() .SetColumns(it => new T() { IsDeleted = true }, true ) //true 支持更新数据过滤器赋值字段一起更新 .Where(exp).ExecuteCommand(); } //建一个接口,继承这个接口的类就能用FalseDelete public interface IDeleted { bool IsDeleted { get ; set ; } } |
9、初始化表
表中数据全部清空,清除,自增初始化
db.DbMaintenance.TruncateTable<T>() |
10、导航删除
db.DeleteNav<OperatorInfo>(x=>x.id==1) .Include(x => x.Roles) .ExecuteCommand(); |
具体用法:https://www.donet5.com/Home/Doc?typeId=24314
11、常用案例1:除最新N条清空
db.Deleteable<Order>().In(it => it.Id, db.Queryable<Order>() .Skip(10).OrderByDescending(it=>it.CreateTime) .Select(it => it.Id)).ExecuteCommand(); //注意Select不要ToList() , ToList就2次查询了 |
12、常用案例2:N到N条删除
db.Deleteable<Order>().In(it => it.Id, db.Queryable<Order>() .Skip(10).Take(10) .Select(it => it.Id)).ExecuteCommand(); //注意Select不要ToList() , ToList就2次查询了 |
13、常用案例3:大数据删除
db.Deleteable<Order>(list).PageSize(500).ExecuteCommand(); |