mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-09 18:00:56 +08:00
update
12
DbContext.md
12
DbContext.md
@@ -2,8 +2,6 @@ FreeSql.DbContext 实现类似 EFCore 使用方法,跟踪对象状态,最终
|
||||
|
||||
目前是第二个初版,已实现状态跟踪保存(导航属性的跟踪暂时不支持)。
|
||||
|
||||
另外还缺少 rowversion 行锁机制,这个现实好就可以在项目中使用了。
|
||||
|
||||
## 安装
|
||||
|
||||
> dotnet add package FreeSql.DbContext
|
||||
@@ -112,6 +110,16 @@ public ValuesController(SongContext songContext) {
|
||||
|
||||
OnConfiguring > AddFreeDbContext
|
||||
|
||||
## 乐观锁
|
||||
|
||||
更新实体数据,在并发情况下极容易造成旧数据将新的记录更新。FreeSql 核心部分已经支持乐观锁。
|
||||
|
||||
乐观锁的原理,是利用实体某字段,如:long version,更新前先查询数据,此时 version 为 1,更新时产生的 SQL 会附加 where version = 1,当修改失败时(即 Affrows == 0)抛出异常。
|
||||
|
||||
每个实体只支持一个乐观锁,在属性前标记特性:[Column(IsVersion = true)] 即可。
|
||||
|
||||
> 无论是使用 FreeSql/FreeSql.Repository/FreeSql.DbContext,每次更新 version 的值都会增加 1
|
||||
|
||||
## 说明
|
||||
|
||||
- DbContext 操作的数据在最后 SaveChanges 时才批量保存;
|
||||
|
||||
Reference in New Issue
Block a user