FreeSql支持豐富的更新數據方法,支持單條或批量更新,在特定的數據庫執行還能夠返回更新後的記錄值。html
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; } }
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.Update<Topic>().SetSource(items).ExecuteAffrows(); //UPDATE `tb_topic` SET `Clicks` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1 WHEN 3 THEN ?p_2 WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6 WHEN 8 THEN ?p_7 WHEN 9 THEN ?p_8 WHEN 10 THEN ?p_9 END, `Title` = CASE `Id` WHEN 1 THEN ?p_10 WHEN 2 THEN ?p_11 WHEN 3 THEN ?p_12 WHEN 4 THEN ?p_13 WHEN 5 THEN ?p_14 WHEN 6 THEN ?p_15 WHEN 7 THEN ?p_16 WHEN 8 THEN ?p_17 WHEN 9 THEN ?p_18 WHEN 10 THEN ?p_19 END, `CreateTime` = CASE `Id` WHEN 1 THEN ?p_20 WHEN 2 THEN ?p_21 WHEN 3 THEN ?p_22 WHEN 4 THEN ?p_23 WHEN 5 THEN ?p_24 WHEN 6 THEN ?p_25 WHEN 7 THEN ?p_26 WHEN 8 THEN ?p_27 WHEN 9 THEN ?p_28 WHEN 10 THEN ?p_29 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
批量更新的場景,先查詢20條記錄,根據本地很複雜的規則把集合的值改完後mysql
傳統作法是循環20次保存,用 case when 只要一次就行sql
fsql.Update<Topic>().SetSource(items).IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ExecuteAffrows(); //UPDATE `tb_topic` SET `Title` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1 WHEN 3 THEN ?p_2 WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6 WHEN 8 THEN ?p_7 WHEN 9 THEN ?p_8 WHEN 10 THEN ?p_9 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
fsql.Update<Topic>().SetSource(items).Set(a => a.CreateTime, DateTime.Now).ExecuteAffrows(); //UPDATE `tb_topic` SET `CreateTime` = ?p_0 WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
指定列更新後,批量保存將失效數據庫
當更新大批量數據時,內部採用分割分批執行的邏輯進行。分割規則以下:api
數量 | 參數量 | |
---|---|---|
MySql | 5000 | 3000 |
PostgreSQL | 5000 | 3000 |
SqlServer | 1000 | 2100 |
Oracle | 500 | 999 |
Sqlite | 5000 | 999 |
數據:爲每批分割的大小,如批量插入 10000 條數據,在 mysql 執行時會分割爲兩批。
參數量:爲每批分割的參數量大小,如批量插入 10000 條數據,每行須要使用 5 個參數化,在 mysql 執行時會分割爲每批 3000 / 5。ui
分割執行後,當外部未提供事務時,內部自開事務,實現插入完整性。this
FreeSql 適配了每一種數據類型參數化,和不參數化的使用。批量插入建議關閉參數化功能,使用 .NonoParameter() 進行執行。code
方法 | 返回值 | 參數 | 描述 |
---|---|---|---|
SetSource | <this> | T1 | IEnumerable
|
更新數據,設置更新的實體 |
IgnoreColumns | <this> | Lambda | 忽略的列 |
Set | <this> | Lambda, value | 設置列的新值,Set(a => a.Name, "newvalue") |
Set | <this> | Lambda | 設置列的的新值爲基礎上增長,Set(a => a.Clicks + 1),至關於 clicks=clicks+1; |
SetRaw | <this> | string, parms | 設置值,自定義SQL語法,SetRaw("title = ?title", new { title = "newtitle" }) |
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語句,返回影響的行數 | |
ExecuteUpdated | List<T1> | 執行SQL語句,返回更新後的記錄 |
(一)入門htm
(十四)批量更新數據