mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-06 00:10:55 +08:00
Updated Update Data (markdown)
@@ -84,26 +84,25 @@ Method 1: (recommended)
|
|||||||
var repo = fsql.GetRepository<Topic>();
|
var repo = fsql.GetRepository<Topic>();
|
||||||
var item = repo.Where(a => a.Id == 1).First(); //Snapshot item at this time
|
var item = repo.Where(a => a.Id == 1).First(); //Snapshot item at this time
|
||||||
item.Title = "newtitle";
|
item.Title = "newtitle";
|
||||||
repo.Update(item); //对比快照时的变化
|
repo.Update(item); //Compare the changes before and after the snapshot.
|
||||||
//UPDATE `Topic` SET `Title` = @p_0
|
//UPDATE `Topic` SET `Title` = @p_0
|
||||||
//WHERE (`Id` = 1)
|
//WHERE (`Id` = 1)
|
||||||
```
|
```
|
||||||
|
|
||||||
> 是不是觉得先查询再更新,啰嗦?
|
> Do you think it’s verbose to query first and then update?
|
||||||
```csharp
|
```csharp
|
||||||
var repo = fsql.GetRepository<Topic>();
|
var repo = fsql.GetRepository<Topic>();
|
||||||
var item = new Topic { Id = 1 };
|
var item = new Topic { Id = 1 };
|
||||||
repo.Attach(item); //此时快照 item
|
repo.Attach(item); //Snapshot item at this time
|
||||||
item.Title = "newtitle";
|
item.Title = "newtitle";
|
||||||
repo.Update(item); //对比快照时的变化
|
repo.Update(item); //Compare the changes before and after the snapshot.
|
||||||
//UPDATE `Topic` SET `Title` = @p_0
|
//UPDATE `Topic` SET `Title` = @p_0
|
||||||
//WHERE (`Id` = 1)
|
//WHERE (`Id` = 1)
|
||||||
```
|
```
|
||||||
|
Method 2: (Original)
|
||||||
方法2:(原始)
|
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
//v1.5.0 忽略更新 null 值的属性
|
//v1.5.0 Ignore properties that update null values
|
||||||
fsql.Update<Topic>()
|
fsql.Update<Topic>()
|
||||||
.SetSourceIgnore(item, col => col == null)
|
.SetSourceIgnore(item, col => col == null)
|
||||||
.ExecuteAffrows();
|
.ExecuteAffrows();
|
||||||
@@ -157,19 +156,19 @@ fsql.Update<Topic>()
|
|||||||
//WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
|
//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
|
```csharp
|
||||||
fsql.Update<Topic>()
|
fsql.Update<Topic>()
|
||||||
.SetRaw("Title = @title", new { title = "新标题" })
|
.SetRaw("Title = @title", new { title = "New Title" })
|
||||||
.Where("Id = @id", 1)
|
.Where("Id = @id", 1)
|
||||||
.ExecuteAffrows();
|
.ExecuteAffrows();
|
||||||
//UPDATE `Topic` SET Title = @title WHERE (Id = @id)
|
//UPDATE `Topic` SET Title = @title WHERE (Id = @id)
|
||||||
```
|
```
|
||||||
|
|
||||||
## 5、根据 Dto 更新
|
## 5. Update According to the DTO
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
fsql.Update<T>()
|
fsql.Update<T>()
|
||||||
@@ -184,19 +183,19 @@ fsql.Update<T>()
|
|||||||
.ExecuteAffrows();
|
.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)。
|
乐观锁的原理,是利用实体某字段,如:long version,更新前先查询数据,此时 version 为 1,更新时产生的 SQL 会附加 where version = 1,当修改失败时(即 Affrows == 0)抛出异常(DbUpdateVersionException)。
|
||||||
|
|
||||||
@@ -204,7 +203,7 @@ fsql.Update<T>()
|
|||||||
|
|
||||||
> 适用 SetSource 更新,每次更新 version 的值都会增加 1
|
> 适用 SetSource 更新,每次更新 version 的值都会增加 1
|
||||||
|
|
||||||
## 8、悲观锁
|
## 8. Pessimistic Lock
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var user = fsql.Select<User>()
|
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)
|
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:
|
||||||
|
|
||||||
- 更新前可预览测试数据,防止错误更新操作;
|
- Data can be previewed before updating to prevent wrong update operations;
|
||||||
- 支持复杂的更新操作,例如:ISelect 上使用 Limit(10) 更新附合条件的前 10 条记录;
|
- 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)
|
- [《数据库事务》](%e4%ba%8b%e5%8a%a1)
|
||||||
- [《学习FreeSql之一:添加数据》](%e6%b7%bb%e5%8a%a0)
|
- [《学习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
|
# API
|
||||||
|
|
||||||
| 方法 | 返回值 | 参数 | 描述 |
|
| Methods | Return | Parameters | Description |
|
||||||
| - | - | - | - |
|
| - | - | - | - |
|
||||||
| SetSource | \<this\> | T1 \| IEnumerable\<T1\> | 更新数据,设置更新的实体 |
|
| SetSource | \<this\> | T1 \| IEnumerable\<T1\> | 更新数据,设置更新的实体 |
|
||||||
| IgnoreColumns | \<this\> | Lambda | 忽略的列 |
|
| IgnoreColumns | \<this\> | Lambda | 忽略的列 |
|
||||||
|
|||||||
Reference in New Issue
Block a user