using SqlSugar; namespace ServiceCenter.Extensions { public static class SqlExtension { /// /// 不添加共享锁和排它锁,当这个选项生效后,可能读到未提交读的数据或“脏数据”,这个选项仅仅应用于SELECT语句 /// /// /// /// public static ISugarQueryable NoLock(this ISugarQueryable obj) { return obj.With(SqlWith.NoLock); } /// /// 跳过已经加锁的数据行,这个选项将使事务读取数据时跳过那些已经被其他事务锁定的数据行,而不是阻塞直到其他事务释放锁,ReadPast仅仅应用于READ COMMITTED隔离性级别下事务操作中的SELECT语句操作。 /// /// /// /// public static ISugarQueryable ReadPast(this ISugarQueryable obj) { return obj.With(SqlWith.ReadPast); } /// /// 指定在读表中数据时设置更新锁(update lock)而不是设置共享锁,该锁一直保持到这个语句或整个事务结束,使用UpdLock的作用是允许用户先读取数据(而且不阻塞其他用户读数据),并且保证在后来再更新数据时,这一段时间内这些数据没有被其他用户修改。 /// 默认持有行级更新锁 /// /// /// /// public static ISugarQueryable UpdLock(this ISugarQueryable obj) { return obj.With("WITH(ROWLOCK)"); } /// /// 跳过加锁数据行并指定在读表中数据时设置更新锁(update lock)而不是设置共享锁,该锁一直保持到这个语句或整个事务结束,使用UpdLock的作用是允许用户先读取数据(而且不阻塞其他用户读数据),并且保证在后来再更新数据时,这一段时间内这些数据没有被其他用户修改。 /// 默认持有行级更新锁 /// /// /// /// public static ISugarQueryable ReadPastUpdLock(this ISugarQueryable obj) { return obj.With("WITH(READPAST,ROWLOCK)"); } /// /// 使用行级锁,而不使用粒度更粗的页级锁和表级锁。 /// /// /// /// public static ISugarQueryable RowLock(this ISugarQueryable obj) { return obj.With(SqlWith.RowLock); } /// /// 更新时使用行级锁 /// /// /// /// /// public static IUpdateable UpdateableRowLock(this SqlSugarScopeProvider scope, T updateObj) where T : class, new() => scope.ScopedContext.Updateable(updateObj).With(SqlWith.RowLock); /// /// 更新时使用行级锁 /// /// /// /// /// public static IUpdateable UpdateableRowLock(this SqlSugarScopeProvider scope, List updateObj) where T : class, new() => scope.ScopedContext.Updateable(updateObj).With(SqlWith.RowLock); /// /// 插入时使用行级锁 /// /// /// /// /// public static IInsertable InsertableRowLock(this SqlSugarScopeProvider scope, T insertObj) where T : class, new() => scope.ScopedContext.Insertable(insertObj).With(SqlWith.RowLock); /// /// 插入时使用行级锁 /// /// /// /// /// public static IInsertable InsertableRowLock(this SqlSugarScopeProvider scope, List insertObj) where T : class, new() => scope.ScopedContext.Insertable(insertObj).With(SqlWith.RowLock); /// /// 删除时使用行级锁 /// /// /// /// /// public static IDeleteable DeleteableRowLock(this SqlSugarScopeProvider scope, T deleteObj) where T : class, new() => scope.ScopedContext.Deleteable(deleteObj).With(SqlWith.RowLock); /// /// 删除时使用行级锁 /// /// /// /// /// public static IDeleteable DeleteableRowLock(this SqlSugarScopeProvider scope, List deleteObj) where T : class, new() => scope.ScopedContext.Deleteable(deleteObj).With(SqlWith.RowLock); } }