FreeSql (十一)更新数据 Where

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, connectionString)
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库
    .Build(); //请务必定义成 Singleton 单例模式

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; }
}

动态条件

fsql.Update<Topic>(object dywhere)

dywhere 支持

  • 主键值
  • new[]
  • Topic对象
  • new[]
  • new

其他条件

除了上面介绍的 dywhere 构造参数外,还支持 Where lambda/sql 方法

出于安全考虑,没有条件不执行更新动作,避免误更新全表数据 更新全表数据:fsql.Update<T>().Where("1=1").Set(a => a.Xxx == xxx).ExecuteAffrows()

fsql.Update<Topic>()
  .Set(a => a.Title, "新标题")
  .Set(a => a.Time, DateTime.Now)
  .Where(a => a.Id == 1)
  .ExecuteAffrows();
//UPDATE `Topic` SET `Title` = @Title, `Time` = @Time 
//WHERE (Id = 1)

API

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

系列文章导航

posted @   FreeSql  阅读(10142)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示
主题色彩