Updated Update Data (markdown)

AlexLEWIS
2021-08-11 11:21:37 +08:00
parent 16b6979c79
commit 447d39208c

@@ -84,26 +84,25 @@ Method 1: (recommended)
var repo = fsql.GetRepository<Topic>();
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 its verbose to query first and then update?
```csharp
var repo = fsql.GetRepository<Topic>();
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<Topic>()
.SetSourceIgnore(item, col => col == null)
.ExecuteAffrows();
@@ -157,19 +156,19 @@ fsql.Update<Topic>()
//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<Topic>()
.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<T>()
@@ -184,19 +183,19 @@ fsql.Update<T>()
.ExecuteAffrows();
```
## 6Set/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<T>()
> 适用 SetSource 更新,每次更新 version 的值都会增加 1
## 8、悲观锁
## 8. Pessimistic Lock
```csharp
var user = fsql.Select<User>()
@@ -236,12 +235,12 @@ fsql.Select<T1>().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 | \<this\> | T1 \| IEnumerable\<T1\> | 更新数据,设置更新的实体 |
| IgnoreColumns | \<this\> | Lambda | 忽略的列 |