diff --git a/Examples/base_entity/Program.cs b/Examples/base_entity/Program.cs index ae01c447..bcb43906 100644 --- a/Examples/base_entity/Program.cs +++ b/Examples/base_entity/Program.cs @@ -487,6 +487,11 @@ namespace base_entity BaseEntity.Initialization(fsql, () => _asyncUow.Value); #endregion + using (var uow = fsql.CreateUnitOfWork()) + { + uow.Orm.Select().ForUpdate().ToList(); + } + var listaaaddd = new List(); for (int i = 0; i < 2; i++) { diff --git a/FreeSql.DbContext/DbContext/DbContextScopedFreeSql.cs b/FreeSql.DbContext/DbContext/DbContextScopedFreeSql.cs index 068d53d1..bad76689 100644 --- a/FreeSql.DbContext/DbContext/DbContextScopedFreeSql.cs +++ b/FreeSql.DbContext/DbContext/DbContextScopedFreeSql.cs @@ -70,7 +70,10 @@ namespace FreeSql { var db = _resolveDbContext?.Invoke(); db?.FlushCommand(); - var select = _originalFsql.Select().WithTransaction(_resolveUnitOfWork?.Invoke()?.GetOrBeginTransaction(false)); + var uow = _resolveUnitOfWork?.Invoke(); + var uowIsolationLevel = uow?.IsolationLevel ?? IsolationLevel.Unspecified; + var select = _originalFsql.Select().WithTransaction(uow?.GetOrBeginTransaction(uowIsolationLevel != IsolationLevel.Unspecified)); + (select as Select0Provider)._resolveHookTransaction = () => uow?.GetOrBeginTransaction(); if (db?.Options.EnableGlobalFilter == false) select.DisableGlobalFilter(); return select; } diff --git a/FreeSql.DbContext/DbSet/DbSet.cs b/FreeSql.DbContext/DbSet/DbSet.cs index 59f5a5cc..e3613bed 100644 --- a/FreeSql.DbContext/DbSet/DbSet.cs +++ b/FreeSql.DbContext/DbSet/DbSet.cs @@ -9,6 +9,7 @@ using System.Linq.Expressions; using System.Reflection; using System.Threading; using FreeSql.Internal.CommonProvider; +using System.Data; namespace FreeSql { @@ -34,7 +35,9 @@ namespace FreeSql protected virtual ISelect OrmSelect(object dywhere) { DbContextFlushCommand(); //查询前先提交,否则会出脏读 - var select = _db.OrmOriginal.Select().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction(false)).TrackToList(TrackToList).WhereDynamic(dywhere); + var uowIsolationLevel = _uow?.IsolationLevel ?? IsolationLevel.Unspecified; + var select = _db.OrmOriginal.Select().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction(uowIsolationLevel != IsolationLevel.Unspecified)).TrackToList(TrackToList).WhereDynamic(dywhere); + (select as Select0Provider)._resolveHookTransaction = () => _uow?.GetOrBeginTransaction(); if (_db.Options.EnableGlobalFilter == false) select.DisableGlobalFilter(); return select; } diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs index ecf4b817..f2ac8e0c 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs @@ -47,6 +47,7 @@ namespace FreeSql.Internal.CommonProvider public Func _cancel; public bool _is_AsTreeCte; public BaseDiyMemberExpression _diymemexpWithTempQuery; + public Func _resolveHookTransaction; public bool IsDefaultSqlContent => _distinct == false && _is_AsTreeCte == false && _tables.Count == 1 && _where.Length == 0 && _join.Length == 0 && string.IsNullOrWhiteSpace(_orderby) && string.IsNullOrWhiteSpace(_groupby) && string.IsNullOrWhiteSpace(_tosqlAppendContent) && @@ -1187,8 +1188,9 @@ namespace FreeSql.Internal.CommonProvider } public TSelect ForUpdate(bool noawait = false) { - if (_transaction == null && _orm.Ado.TransactionCurrentThread == null) - throw new Exception($"{CoreStrings.Begin_Transaction_Then_ForUpdate}"); + if (_transaction == null && _orm.Ado.TransactionCurrentThread != null) this.WithTransaction(_orm.Ado.TransactionCurrentThread); + if (_transaction == null && _resolveHookTransaction != null) this.WithTransaction(_resolveHookTransaction()); + if (_transaction == null) throw new Exception($"{CoreStrings.Begin_Transaction_Then_ForUpdate}"); switch (_orm.Ado.DataType) { case DataType.MySql: