FreeSql (十一)更新數據 Where


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

static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, connstr)
    .UseAutoSyncStructure(true) //自動同步實體結構到數據庫
    .Build(); //請務必定義成 Singleton 單例模式

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

動態條件

Update<Topic>(object dywhere)

dywhere 支持

  • 主鍵值
  • new[] { 主鍵值1, 主鍵值2 }
  • Topic對象
  • new[] { Topic對象1, Topic對象2 }
  • new { id = 1 }

其他條件

除了上面介紹的 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 `tb_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語句,返回更新后的記錄

系列文章導航


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM