From 713ef26909d99b5e12a98cf23ac302e5f18873eb Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Fri, 8 May 2020 14:52:28 +0800 Subject: [PATCH] update BaseEntity readme --- Examples/base_entity/readme.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Examples/base_entity/readme.md b/Examples/base_entity/readme.md index a9ace8ac..6a8bde3b 100644 --- a/Examples/base_entity/readme.md +++ b/Examples/base_entity/readme.md @@ -90,3 +90,37 @@ var items = UserGroup.Where(a => a.Id > 10).ToList(); 支持多表查询时,软删除条件会附加在每个表中; > 有关更多查询方法,请参考资料:https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2 + +# 事务建议 + +由于 AsyncLocal 平台兼容不好,所以交给外部管理。 + +```csharp +static AsyncLocal _asyncUow = new AsyncLocal(); + +BaseEntity.Initialization(fsql, () => _asyncUow.Value); +``` + +在 Scoped 开始时: _asyncUow.Value = fsql.CreateUnitOfWork(); (也可以使用 UnitOfWorkManager 对象获取 uow) + +在 Scoped 结束时:_asyncUow.Value = null; + +如下: + +```csharp +using (var uow = fsql.CreateUnitOfWork()) +{ + _asyncUow.Value = uow; + + try + { + //todo ... + } + finally + { + _asyncUow.Value = null; + } + + uow.Commit(); +} +```