删除是一个非常危险的操作,FreeSql对删除支持并不强大,仅支持了单表有条件的删除方法。

不想过多的介绍拉长删除数据的系列文章,删除数据的介绍仅此一篇。

若Where条件为空的时候执行方法,FreeSql仅返回0或默认值,不执行真正的SQL删除操作。

为了增强系统的安全性,强烈建议在实体中增加 is_deledted 字段做软删除标识。
var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" +
"Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10"; IFreeSql
fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, connstr)
.UseAutoSyncStructure(true) //自动同步实体结构到数据库 .Build(); [Table(Name = "tb_topic")]
class Topic { [Column(IsIdentity = true, IsPrimary = true)] public int Id {
get; set; } public int Clicks { get; set; } public string Title { get; set; }
public DateTime CreateTime { get; set; } }
动态条件
Delete<Topic>(object dywhere)
dywhere 支持

* 主键值
* new[] { 主键值1, 主键值2 }
* Topic对象
* new[] { Topic对象1, Topic对象2 }
* new { id = 1 } fsql.Delete<Topic>(new[] { 1, 2 }).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` = 1 OR `Id` = 2) fsql.Delete<Topic>(new
Topic { Id = 1, Title = "test" }).ExecuteAffrows(); //DELETE FROM `tb_topic`
WHERE (`Id` = 1) fsql.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test"
}, new Topic { Id = 2, Title = "test" } }).ExecuteAffrows(); //DELETE FROM
`tb_topic` WHERE (`Id` = 1 OR `Id` = 2) fsql.Delete<Topic>(new { id = 1
}).ExecuteAffrows(); //DELETE FROM `tb_topic` WHERE (`Id` = 1)
删除条件

出于安全考虑,没有条件不执行删除动作,避免误删除全表数据
删除全表数据:fsql.Delete<T>().Where("1=1").ExecuteAffrows()
fsql.Delete<Topic>().Where(a => a.Id == 1).ExecuteAffrows(); //DELETE FROM
`tb_topic` WHERE (`Id` = 1) fsql.Delete<Topic>().Where("id = ?id", new { id = 1
}).ExecuteAffrows(); //DELETE FROM `tb_topic` WHERE (id = ?id) var item = new
Topic { Id = 1, Title = "newtitle" };
fsql.Delete<Topic>().Where(item).ExecuteAffrows(); //DELETE FROM `tb_topic`
WHERE (`Id` = 1) var items = new List<Topic>(); for (var a = 0; a < 10; a++)
items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
fsql.Delete<Topic>().Where(items).ExecuteAffrows(); //DELETE FROM `tb_topic`
WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
API

方法 返回值 参数 描述
Where <this> Lambda 表达式条件,仅支持实体基础成员(不包含导航对象)
Where <this> string, parms 原生sql语法条件,Where("id = ?id", new { id = 1 })
Where <this> T1 | IEnumerable 传入实体或集合,将其主键作为条件
WhereExists <this> ISelect 子查询是否存在
WithTransaction <this> DbTransaction 设置事务对象
ToSql string 返回即将执行的SQL语句
ExecuteAffrows long 执行SQL语句,返回影响的行数
ExecuteDeleted List<T1> 执行SQL语句,返回被删除的记录
系列文章导航

*
(一)入门 <https://www.cnblogs.com/FreeSql/p/11531300.html>

*
(二)自动迁移实体 <https://www.cnblogs.com/FreeSql/p/11531301.html>

*
(三)实体特性 <https://www.cnblogs.com/FreeSql/p/11531302.html>

*
(四)实体特性 Fluent Api <https://www.cnblogs.com/FreeSql/p/11531304.html>

*
(五)插入数据 <https://www.cnblogs.com/FreeSql/p/11531306.html>

*
(六)批量插入数据 <https://www.cnblogs.com/FreeSql/p/11531309.html>

*
(七)插入数据时忽略列 <https://www.cnblogs.com/FreeSql/p/11531316.html>

*
(八)插入数据时指定列 <https://www.cnblogs.com/FreeSql/p/11531318.html>

*
(九)删除数据

*
(十)更新数据 <https://www.cnblogs.com/FreeSql/p/11531321.html>

*
(十一)更新数据 Where <https://www.cnblogs.com/FreeSql/p/11531324.html>

*
(十二)更新数据时指定列 <https://www.cnblogs.com/FreeSql/p/11531327.html>

*
(十三)更新数据时忽略列 <https://www.cnblogs.com/FreeSql/p/11531334.html>

*
(十四)批量更新数据 <https://www.cnblogs.com/FreeSql/p/11531335.html>

*
(十五)查询数据 <https://www.cnblogs.com/FreeSql/p/11531339.html>

*
(十六)分页查询 <https://www.cnblogs.com/FreeSql/p/11531341.html>

*
(十七)联表查询 <https://www.cnblogs.com/FreeSql/p/11531346.html>

*
(十八)导航属性 <https://www.cnblogs.com/FreeSql/p/11531352.html>

*
(十九)多表查询 <https://www.cnblogs.com/FreeSql/p/11531362.html>

*
(二十)多表查询 WhereCascade <https://www.cnblogs.com/FreeSql/p/11531372.html>

*
(二十一)查询返回数据 <https://www.cnblogs.com/FreeSql/p/11531376.html>

*
(二十二)Dto 映射查询 <https://www.cnblogs.com/FreeSql/p/11531381.html>

*
(二十三)分组、聚合 <https://www.cnblogs.com/FreeSql/p/11531384.html>

*
(二十四)Linq To Sql 语法使用介绍 <https://www.cnblogs.com/FreeSql/p/11531392.html>

*
(二十五)延时加载 <https://www.cnblogs.com/FreeSql/p/11531395.html>

*
(二十六)贪婪加载 Include、IncludeMany、Dto、ToList
<https://www.cnblogs.com/FreeSql/p/11531404.html>

*
(二十七)将已写好的 SQL 语句,与实体类映射进行二次查询
<https://www.cnblogs.com/FreeSql/p/11531416.html>

*
(二十八)事务 <https://www.cnblogs.com/FreeSql/p/11531423.html>

*
(二十九)Lambda 表达式 <https://www.cnblogs.com/FreeSql/p/11531425.html>

*
(三十)读写分离 <https://www.cnblogs.com/FreeSql/p/11531430.html>

*
(三十一)分区分表 <https://www.cnblogs.com/FreeSql/p/11531435.html>

*
(三十二)Aop <https://www.cnblogs.com/FreeSql/p/11531471.html>

*
(三十三)CodeFirst 类型映射 <https://www.cnblogs.com/FreeSql/p/11531543.html>

*
(三十四)CodeFirst 迁移说明 <https://www.cnblogs.com/FreeSql/p/11531550.html>

*
(三十五)CodeFirst 自定义特性 <https://www.cnblogs.com/FreeSql/p/11531576.html>