需要配置主键 ,根据主键删除需要给实体配置主键,参考文档实体配置
//单个实体 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 |
注意:请升级到5.1.4.60-preview02
List<T> list=new List<T>(){...}; db.Deleteable<Order>().WhereColumns(list,it=>new { it.Id}).ExecuteCommand();
//2.Object对象插入 (5.1.3.45) //o必须是真实的类对象 (比如根据type反射出来的对象,或者转成了Object的实体) db.DeleteableByObject(o).ExecuteCommand(); //更多功能 :动态建类等 https://www.donet5.com/Home/Doc?typeId=2562
db.Deleteable<Student>().In(1).ExecuteCommand(); //无主键用法 db.Deleteable<Order>().In(it=>it.Id,1).ExecuteCommand();
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以上
db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();
db.Deleteable<Student>() .Where(p => SqlFunc.Subqueryable<School>().Where(s => s.Id == p.SchoolId).Any()) .ExecuteCommand() //.Where(s => s.Id == p.SchoolId) s和p的关联条件不能少,不然就全删了
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);//转成字典
//配置表过滤器 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 )
优点:简单直接用,如果有数据过滤器建议用方式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()
优点:可以结合更新过滤器一起生效
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; } }
表中数据全部清空,清除,自增初始化
db.DbMaintenance.TruncateTable<T>()
db.DeleteNav<OperatorInfo>(x=>x.id==1) .Include(x => x.Roles) .ExecuteCommand();
具体用法:https://www.donet5.com/Home/Doc?typeId=24314
db.Deleteable<Order>().In(it => it.Id, db.Queryable<Order>() .Skip(10).OrderByDescending(it=>it.CreateTime)//Take(10)最新10条2个有区别 .Select(it => it.Id)).ExecuteCommand(); //注意Select不要ToList() , ToList就2次查询了
db.Deleteable<Order>().In(it => it.Id, db.Queryable<Order>() .Skip(10).Take(10) .Select(it => it.Id)).ExecuteCommand(); //注意Select不要ToList() , ToList就2次查询了
db.Deleteable<Order>(list).PageSize(500).ExecuteCommand();
2016 © donet5.comApache Licence 2.0