Updated 修改 (markdown)

AlexLEWIS
2021-08-11 11:45:01 +08:00
parent 0909fcc82a
commit 9e6a6e6993

@@ -21,13 +21,13 @@ class Topic {
```csharp ```csharp
fsql.Update<Topic>(object dywhere) fsql.Update<Topic>(object dywhere)
``` ```
dywhere 支持 `dywhere` 支持
* 主键值 * 主键值
* new[] { 主键值1, 主键值2 } * `new[] { 主键值1, 主键值2 }`
* Topic对象 * Topic 对象
* new[] { Topic对象1, Topic对象2 } * `new[] { Topic对象1, Topic对象2 }`
* new { id = 1 } * `new { id = 1 }`
## 1、更新指定列 ## 1、更新指定列
```csharp ```csharp
@@ -38,7 +38,7 @@ fsql.Update<Topic>(1)
//WHERE (`Id` = 1) //WHERE (`Id` = 1)
``` ```
> 支持 Set() 多次,相当于拼接 > 支持 `Set()` 多次,相当于拼接
```csharp ```csharp
fsql.Update<Topic>(1) fsql.Update<Topic>(1)
@@ -61,9 +61,9 @@ fsql.Update<Topic>(1)
## 2、更新条件 ## 2、更新条件
> 除了上面介绍的 dywhere 构造参数外,还支持 Where lambda/sql 方法 > 除了上面介绍的 `dywhere` 构造参数外,还支持 `Where lambda/sql` 方法
> 出于安全考虑没有条件不执行更新动作避免误更新全表数据。更新全表数据fsql.Update\<T\>().Where("1=1").Set(a => a.Xxx == xxx).ExecuteAffrows() > 出于安全考虑,没有条件不执行更新动作,避免误更新全表数据。更新全表数据:`fsql.Update<T>().Where("1=1").Set(a => a.Xxx == xxx).ExecuteAffrows()`
```csharp ```csharp
fsql.Update<Topic>() fsql.Update<Topic>()
@@ -79,7 +79,7 @@ fsql.Update<Topic>()
方法1(推荐) 方法1(推荐)
> 只更新变化的属性,依赖 FreeSql.Repository > 只更新变化的属性,依赖 `FreeSql.Repository`
```csharp ```csharp
var repo = fsql.GetRepository<Topic>(); var repo = fsql.GetRepository<Topic>();
var item = repo.Where(a => a.Id == 1).First(); //此时快照 item var item = repo.Where(a => a.Id == 1).First(); //此时快照 item
@@ -157,7 +157,7 @@ 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 将失效 > 指定 `Set` 列更新后,`SetSource` 将失效
## 4、自定义SQL ## 4、自定义SQL
@@ -188,21 +188,21 @@ fsql.Update<T>()
他们三个是平级功能,分别对应: 他们三个是平级功能,分别对应:
- Set/SetRaw 在知道实体的时候使用,对应 update t set x = x - `Set/SetRaw` 在知道实体的时候使用,对应 `update t set x = x`
- SetSource 更新整个实体,可以配合 UpdateColumns/IgnoreColumns 指定或忽略字段 - `SetSource` 更新整个实体,可以配合 `UpdateColumns``IgnoreColumns` 指定或忽略字段
- SetDto 是 Set 的批量操作 - `SetDto``Set` 的批量操作
## 7、乐观锁 ## 7、乐观锁
更新整个实体数据时,在并发情况下极容易造成旧数据将新的记录更新。 更新整个实体数据时,在并发情况下极容易造成旧数据将新的记录更新。
乐观锁的原理是利用实体某字段long version更新前先查询数据此时 version 为 1,更新时产生的 SQL 会附加 where version = 1当修改失败时即 Affrows == 0抛出异常DbUpdateVersionException 乐观锁的原理,是利用实体某字段,如:`long version`,更新前先查询数据,此时 `version``1`,更新时产生的 SQL 会附加 `where version = 1`,当修改失败时(即 `Affrows == 0`抛出异常DbUpdateVersionException
每个实体只支持一个乐观锁属性,在属性前标记特性:[Column(IsVersion = true)] 即可。 每个实体只支持一个乐观锁属性,在属性前标记特性:`[Column(IsVersion = true)]` 即可。
> 适用 SetSource 更新,每次更新 version 的值都会增加 1 > 适用 `SetSource` 更新,每次更新 `version` 的值都会增加 `1`
## 8、悲观锁 ## 8、悲观锁
@@ -214,7 +214,7 @@ var user = fsql.Select<User>()
//SELECT ... FROM User a for update nowait //SELECT ... FROM User a for update nowait
``` ```
for update 在 Oracle/PostgreSQL/MySql 是通用的写法,我们对 SqlServer 做了特别适配,执行的 SQL 语句大致如下: `ForUpdate` 在 Oracle/PostgreSQL/MySql 是通用的写法,我们对 SqlServer 做了特别适配,执行的 SQL 语句大致如下:
```sql ```sql
SELECT ... FROM [User] a With(UpdLock, RowLock, NoWait) SELECT ... FROM [User] a With(UpdLock, RowLock, NoWait)
@@ -222,7 +222,7 @@ SELECT ... FROM [User] a With(UpdLock, RowLock, NoWait)
## 9、ISelect.ToUpdate 高级更新 ## 9、ISelect.ToUpdate 高级更新
IUpdate 默认不支持导航对象多表关联等。ISelect.ToUpdate 可将查询转为 IUpdate以便使用导航对象更新数据如下 `IUpdate` 默认不支持导航对象,多表关联等。`ISelect.ToUpdate` 可将查询转为 `IUpdate`,以便使用导航对象更新数据,如下:
```csharp ```csharp
fsql.Select<T1>().Where(a => a.Options.xxx == 1) fsql.Select<T1>().Where(a => a.Options.xxx == 1)
@@ -239,7 +239,7 @@ UPDATE `T1` SET Title = '111' WHERE id in (select a.id from T1 a left join Optio
复杂更新使用该方案的好处: 复杂更新使用该方案的好处:
- 更新前可预览测试数据,防止错误更新操作; - 更新前可预览测试数据,防止错误更新操作;
- 支持复杂的更新操作例如ISelect 上使用 Limit(10) 更新附合条件的前 10 条记录; - 支持复杂的更新操作,例如:`ISelect` 上使用 `Limit(10)` 更新附合条件的前 10 条记录;
## 参考资料 ## 参考资料
@@ -257,16 +257,16 @@ UPDATE `T1` SET Title = '111' WHERE id in (select a.id from T1 a left join Optio
| - | - | - | - | | - | - | - | - |
| SetSource | \<this\> | T1 \| IEnumerable\<T1\> | 更新数据,设置更新的实体 | | SetSource | \<this\> | T1 \| IEnumerable\<T1\> | 更新数据,设置更新的实体 |
| IgnoreColumns | \<this\> | Lambda | 忽略的列 | | IgnoreColumns | \<this\> | Lambda | 忽略的列 |
| Set | \<this\> | Lambda, value | 设置列的新值Set(a => a.Name, "newvalue") | | Set | \<this\> | Lambda, value | 设置列的新值,`Set(a => a.Name, "newvalue")` |
| Set | \<this\> | Lambda | 设置列的的新值为基础上增加Set(a => a.Clicks + 1),相当于 clicks=clicks+1 | | Set | \<this\> | Lambda | 设置列的的新值为基础上增加,`Set(a => a.Clicks + 1)`,相当于 clicks=clicks+1 |
| SetDto | \<this\> | object | 根据 dto 更新的方法 | | SetDto | \<this\> | object | 根据 DTO 更新的方法 |
| SetRaw | \<this\> | string, parms | 设置值自定义SQL语法SetRaw("title = @title", new { title = "newtitle" }) | | SetRaw | \<this\> | string, parms | 设置值,自定义 SQL 语法,`SetRaw("title = @title", new { title = "newtitle" })` |
| Where | \<this\> | Lambda | 表达式条件,仅支持实体基础成员(不包含导航对象) | | Where | \<this\> | Lambda | 表达式条件,仅支持实体基础成员(不包含导航对象) |
| Where | \<this\> | string, parms | 原生sql语法条件Where("id = @id", new { id = 1 }) | | Where | \<this\> | string, parms | 原生sql语法条件`Where("id = @id", new { id = 1 })` |
| Where | \<this\> | T1 \| IEnumerable\<T1\> | 传入实体或集合,将其主键作为条件 | | Where | \<this\> | T1 \| IEnumerable\<T1\> | 传入实体或集合,将其主键作为条件 |
| CommandTimeout | \<this\> | int | 命令超时设置(秒) | | CommandTimeout | \<this\> | int | 命令超时设置(秒) |
| WithTransaction | \<this\> | DbTransaction | 设置事务对象 | | WithTransaction | \<this\> | DbTransaction | 设置事务对象 |
| WithConnection | \<this\> | DbConnection | 设置连接对象 | | WithConnection | \<this\> | DbConnection | 设置连接对象 |
| ToSql | string | | 返回即将执行的SQL语句 | | ToSql | string | | 返回即将执行的 SQL 语句 |
| ExecuteAffrows | long | | 执行SQL语句返回影响的行数 | | ExecuteAffrows | long | | 执行 SQL 语句,返回影响的行数 |
| ExecuteUpdated | List\<T1\> | | 执行SQL语句返回更新后的记录 | | ExecuteUpdated | List\<T1\> | | 执行 SQL 语句,返回更新后的记录 |