diff --git a/Update-Data.md b/Update-Data.md index 75fd663..992d921 100644 --- a/Update-Data.md +++ b/Update-Data.md @@ -84,26 +84,25 @@ Method 1: (recommended) var repo = fsql.GetRepository(); var item = repo.Where(a => a.Id == 1).First(); //Snapshot item at this time item.Title = "newtitle"; -repo.Update(item); //对比快照时的变化 +repo.Update(item); //Compare the changes before and after the snapshot. //UPDATE `Topic` SET `Title` = @p_0 //WHERE (`Id` = 1) ``` -> 是不是觉得先查询再更新,啰嗦? +> Do you think it’s verbose to query first and then update? ```csharp var repo = fsql.GetRepository(); var item = new Topic { Id = 1 }; -repo.Attach(item); //此时快照 item +repo.Attach(item); //Snapshot item at this time item.Title = "newtitle"; -repo.Update(item); //对比快照时的变化 +repo.Update(item); //Compare the changes before and after the snapshot. //UPDATE `Topic` SET `Title` = @p_0 //WHERE (`Id` = 1) ``` - -方法2:(原始) +Method 2: (Original) ```csharp -//v1.5.0 忽略更新 null 值的属性 +//v1.5.0 Ignore properties that update null values fsql.Update() .SetSourceIgnore(item, col => col == null) .ExecuteAffrows(); @@ -157,19 +156,19 @@ fsql.Update() //WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10)) ``` -> 指定 Set 列更新后,SetSource 将失效 +> After the specified `Set` column is updated, `SetSource` will become invalid -## 4、自定义SQL +## 4. Custom SQL ```csharp fsql.Update() - .SetRaw("Title = @title", new { title = "新标题" }) + .SetRaw("Title = @title", new { title = "New Title" }) .Where("Id = @id", 1) .ExecuteAffrows(); //UPDATE `Topic` SET Title = @title WHERE (Id = @id) ``` -## 5、根据 Dto 更新 +## 5. Update According to the DTO ```csharp fsql.Update() @@ -184,19 +183,19 @@ fsql.Update() .ExecuteAffrows(); ``` -## 6、Set/SetSource/SetDto 区别 +## 6. The difference between Set, SetSource and SetDto -他们三个是平级功能,分别对应: +The three of them are functions of the same level, corresponding to: -- Set/SetRaw 在知道实体的时候使用,对应 update t set x = x +- `Set/SetRaw` is used when the entity is known, corresponding to `update t set x = x` -- SetSource 更新整个实体,可以配合 UpdateColumns/IgnoreColumns 指定或忽略字段 +- `SetSource` updates the entire entity, you can use `UpdateColumns` and/or `IgnoreColumns` to specify or ignore fields -- SetDto 是 Set 的批量操作 +- `SetDto` is a batch operation of `Set` -## 7、乐观锁 +## 7. Optimistic Lock -更新整个实体数据时,在并发情况下极容易造成旧数据将新的记录更新。 +When updating the entire entity data, it is very easy to cause the old data to update the new record in the case of concurrency. 乐观锁的原理,是利用实体某字段,如:long version,更新前先查询数据,此时 version 为 1,更新时产生的 SQL 会附加 where version = 1,当修改失败时(即 Affrows == 0)抛出异常(DbUpdateVersionException)。 @@ -204,7 +203,7 @@ fsql.Update() > 适用 SetSource 更新,每次更新 version 的值都会增加 1 -## 8、悲观锁 +## 8. Pessimistic Lock ```csharp var user = fsql.Select() @@ -236,12 +235,12 @@ fsql.Select().Where(a => a.Options.xxx == 1) UPDATE `T1` SET Title = '111' WHERE id in (select a.id from T1 a left join Options b on b.t1id = a.id where b.xxx = 1) ``` -复杂更新使用该方案的好处: +The benefits of using this program for dang complex update: -- 更新前可预览测试数据,防止错误更新操作; -- 支持复杂的更新操作,例如:ISelect 上使用 Limit(10) 更新附合条件的前 10 条记录; +- Data can be previewed before updating to prevent wrong update operations; +- Support complex update operations, for example: Use `Limit(10)` on `ISelect` to update the first 10 records that meet the conditions; -## 参考资料 +## Reference - [《数据库事务》](%e4%ba%8b%e5%8a%a1) - [《学习FreeSql之一:添加数据》](%e6%b7%bb%e5%8a%a0) @@ -253,7 +252,7 @@ UPDATE `T1` SET Title = '111' WHERE id in (select a.id from T1 a left join Optio # API -| 方法 | 返回值 | 参数 | 描述 | +| Methods | Return | Parameters | Description | | - | - | - | - | | SetSource | \ | T1 \| IEnumerable\ | 更新数据,设置更新的实体 | | IgnoreColumns | \ | Lambda | 忽略的列 |