- 增加 FreeSql.DbContext/Repository Async CancellationToken 参数;#537

This commit is contained in:
2881099 2020-11-17 19:44:47 +08:00
parent c04a32659d
commit 2947572f05
8 changed files with 122 additions and 137 deletions

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
#if net40
@ -12,14 +13,14 @@ namespace FreeSql
{
partial class DbContext
{
async public virtual Task<int> SaveChangesAsync()
async public virtual Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
await FlushCommandAsync();
await FlushCommandAsync(cancellationToken);
return SaveChangesSuccess();
}
static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], Task<int>>>> _dicFlushCommandDbSetBatchAsync = new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], Task<int>>>>();
async internal Task FlushCommandAsync()
static ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>> _dicFlushCommandDbSetBatchAsync = new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>>();
async internal Task FlushCommandAsync(CancellationToken cancellationToken)
{
if (isFlushCommanding) return;
if (_prevCommands.Any() == false) return;
@ -31,25 +32,26 @@ namespace FreeSql
Task<int> dbsetBatch(string method)
{
var tryfunc = _dicFlushCommandDbSetBatchAsync
.GetOrAdd(oldinfo.stateType, stateType => new ConcurrentDictionary<string, Func<object, object[], Task<int>>>())
.GetOrAdd(oldinfo.stateType, stateType => new ConcurrentDictionary<string, Func<object, object[], CancellationToken, Task<int>>>())
.GetOrAdd(method, methodName =>
{
var arrType = oldinfo.stateType.MakeArrayType();
var dbsetType = oldinfo.dbSet.GetType().BaseType;
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType }, null);
var dbsetTypeMethod = dbsetType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { arrType, typeof(CancellationToken) }, null);
var returnTarget = Expression.Label(typeof(Task<int>));
var parm1DbSet = Expression.Parameter(typeof(object));
var parm2Vals = Expression.Parameter(typeof(object[]));
var parm3CancelToken = Expression.Parameter(typeof(CancellationToken));
var var1Vals = Expression.Variable(arrType);
return Expression.Lambda<Func<object, object[], Task<int>>>(Expression.Block(
return Expression.Lambda<Func<object, object[], CancellationToken, Task<int>>>(Expression.Block(
new[] { var1Vals },
Expression.Assign(var1Vals, Expression.Convert(global::FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(arrType, parm2Vals), arrType)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals)),
Expression.Return(returnTarget, Expression.Call(Expression.Convert(parm1DbSet, dbsetType), dbsetTypeMethod, var1Vals, parm3CancelToken)),
Expression.Label(returnTarget, Expression.Default(typeof(Task<int>)))
), new[] { parm1DbSet, parm2Vals }).Compile();
), new[] { parm1DbSet, parm2Vals, parm3CancelToken }).Compile();
});
return tryfunc(oldinfo.dbSet, states.ToArray());
return tryfunc(oldinfo.dbSet, states.ToArray(), cancellationToken);
}
async Task funcDelete()
{

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
#if net40
@ -14,22 +15,22 @@ namespace FreeSql
{
partial class DbSet<TEntity>
{
Task DbContextFlushCommandAsync()
Task DbContextFlushCommandAsync(CancellationToken cancellationToken)
{
_dicUpdateTimes.Clear();
return _db.FlushCommandAsync();
return _db.FlushCommandAsync(cancellationToken);
}
async Task<int> DbContextBatchAddAsync(EntityState[] adds)
async Task<int> DbContextBatchAddAsync(EntityState[] adds, CancellationToken cancellationToken)
{
if (adds.Any() == false) return 0;
var affrows = await this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrowsAsync();
var affrows = await this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrowsAsync(cancellationToken);
_db._entityChangeReport.AddRange(adds.Select(a => new DbContext.EntityChangeReport.ChangeInfo { Object = a.Value, Type = DbContext.EntityChangeType.Insert }));
return affrows;
}
#region Add
async Task AddPrivAsync(TEntity data, bool isCheck)
async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellationToken)
{
if (isCheck && CanAdd(data, true) == false) return;
if (_tableIdentitys.Length > 0)
@ -47,38 +48,38 @@ namespace FreeSql
case DataType.Firebird: //firebird 只支持单条插入 returning
if (_tableIdentitys.Length == 1)
{
await DbContextFlushCommandAsync();
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
await DbContextFlushCommandAsync(cancellationToken);
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync(cancellationToken);
IncrAffrows(1);
_db.OrmOriginal.SetEntityIdentityValueWithPrimary(_entityType, data, idtval);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data, true);
await AddOrUpdateNavigateListAsync(data, true, null, cancellationToken);
}
else
{
await DbContextFlushCommandAsync();
var newval = (await this.OrmInsert(data).ExecuteInsertedAsync()).First();
await DbContextFlushCommandAsync(cancellationToken);
var newval = (await this.OrmInsert(data).ExecuteInsertedAsync(cancellationToken)).First();
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { Object = newval, Type = DbContext.EntityChangeType.Insert });
IncrAffrows(1);
_db.OrmOriginal.MapEntityValue(_entityType, newval, data);
Attach(newval);
if (_db.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data, true);
await AddOrUpdateNavigateListAsync(data, true, null, cancellationToken);
}
return;
default:
if (_tableIdentitys.Length == 1)
{
await DbContextFlushCommandAsync();
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
await DbContextFlushCommandAsync(cancellationToken);
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync(cancellationToken);
IncrAffrows(1);
_db.OrmOriginal.SetEntityIdentityValueWithPrimary(_entityType, data, idtval);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data, true);
await AddOrUpdateNavigateListAsync(data, true, null, cancellationToken);
}
return;
}
@ -86,15 +87,15 @@ namespace FreeSql
EnqueueToDbContext(DbContext.EntityChangeType.Insert, CreateEntityState(data));
Attach(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
await AddOrUpdateNavigateListAsync(data, true);
await AddOrUpdateNavigateListAsync(data, true, null, cancellationToken);
}
public Task AddAsync(TEntity data) => AddPrivAsync(data, true);
async public Task AddRangeAsync(IEnumerable<TEntity> data)
public Task AddAsync(TEntity data, CancellationToken cancellationToken = default) => AddPrivAsync(data, true, cancellationToken);
async public Task AddRangeAsync(IEnumerable<TEntity> data, CancellationToken cancellationToken = default)
{
if (CanAdd(data, true) == false) return;
if (data.ElementAtOrDefault(1) == default(TEntity))
{
await AddAsync(data.First());
await AddAsync(data.First(), cancellationToken);
return;
}
if (_tableIdentitys.Length > 0)
@ -109,8 +110,8 @@ namespace FreeSql
case DataType.KingbaseES:
case DataType.OdbcKingbaseES:
case DataType.ShenTong:
await DbContextFlushCommandAsync();
var rets = await this.OrmInsert(data).ExecuteInsertedAsync();
await DbContextFlushCommandAsync(cancellationToken);
var rets = await this.OrmInsert(data).ExecuteInsertedAsync(cancellationToken);
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_db.OrmOriginal.Ado.DataType} 的返回数据,与添加的数目不匹配");
_db._entityChangeReport.AddRange(rets.Select(a => new DbContext.EntityChangeReport.ChangeInfo { Object = a, Type = DbContext.EntityChangeType.Insert }));
var idx = 0;
@ -120,11 +121,11 @@ namespace FreeSql
AttachRange(rets);
if (_db.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
await AddOrUpdateNavigateListAsync(item, true);
await AddOrUpdateNavigateListAsync(item, true, null, cancellationToken);
return;
default:
foreach (var s in data)
await AddPrivAsync(s, false);
await AddPrivAsync(s, false, cancellationToken);
return;
}
}
@ -136,11 +137,11 @@ namespace FreeSql
AttachRange(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
await AddOrUpdateNavigateListAsync(item, true);
await AddOrUpdateNavigateListAsync(item, true, null, cancellationToken);
}
}
async public Task SaveManyAsync(TEntity item, string propertyName)
async public Task SaveManyAsync(TEntity item, string propertyName, CancellationToken cancellationToken = default)
{
if (item == null) return;
if (string.IsNullOrEmpty(propertyName)) return;
@ -156,15 +157,15 @@ namespace FreeSql
throw new ArgumentException($"{_table.Type.FullName} 类型的属性 {propertyName} 不是 OneToMany 或 ManyToMany 特性");
}
await DbContextFlushCommandAsync();
await DbContextFlushCommandAsync(cancellationToken);
var oldEnable = _db.Options.EnableAddOrUpdateNavigateList;
_db.Options.EnableAddOrUpdateNavigateList = false;
try
{
await AddOrUpdateNavigateListAsync(item, false, propertyName);
await AddOrUpdateNavigateListAsync(item, false, propertyName, cancellationToken);
if (tref.RefType == Internal.Model.TableRefType.OneToMany)
{
await DbContextFlushCommandAsync();
await DbContextFlushCommandAsync(cancellationToken);
//删除没有保存的数据,求出主体的条件
var deleteWhereParentParam = Expression.Parameter(typeof(object), "a");
Expression whereParentExp = null;
@ -189,7 +190,7 @@ namespace FreeSql
subDelete.WhereDynamic(propValEach, true);
break;
}
await subDelete.ExecuteAffrowsAsync();
await subDelete.ExecuteAffrowsAsync(cancellationToken);
}
}
finally
@ -197,7 +198,7 @@ namespace FreeSql
_db.Options.EnableAddOrUpdateNavigateList = oldEnable;
}
}
async Task AddOrUpdateNavigateListAsync(TEntity item, bool isAdd, string propertyName = null)
async Task AddOrUpdateNavigateListAsync(TEntity item, bool isAdd, string propertyName, CancellationToken cancellationToken)
{
Func<PropertyInfo, Task> action = async prop =>
{
@ -225,9 +226,9 @@ namespace FreeSql
curList.Add(propValItem);
var flagExists = refSet.ExistsInStates(propValItem);
if (flagExists == false)
flagExists = await refSet.Select.WhereDynamic(propValItem).AnyAsync();
flagExists = await refSet.Select.WhereDynamic(propValItem).AnyAsync(cancellationToken);
if (refSet.CanAdd(propValItem, false) && flagExists != true)
await refSet.AddAsync(propValItem);
await refSet.AddAsync(propValItem, cancellationToken);
}
var midSelectParam = Expression.Parameter(typeof(object), "a");
var midWheres = new List<Expression<Func<object, bool>>>();
@ -246,7 +247,7 @@ namespace FreeSql
.WithTransaction(_uow?.GetOrBeginTransaction());
foreach (var midWhere in midWheres) delall.Where(midWhere);
var sql = delall.ToSql();
await delall.ExecuteAffrowsAsync();
await delall.ExecuteAffrowsAsync(cancellationToken);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { Object = sql, Type = DbContext.EntityChangeType.SqlRaw });
}
else //保存
@ -257,7 +258,7 @@ namespace FreeSql
{
var midSelect = midSet.Select;
foreach (var midWhere in midWheres) midSelect.Where(midWhere);
midList = await midSelect.ToListAsync();
midList = await midSelect.ToListAsync(false, cancellationToken);
}
else
midList = new List<object>();
@ -307,7 +308,7 @@ namespace FreeSql
}
midListAdd.Add(newItem);
}
await midSet.AddRangeAsync(midListAdd);
await midSet.AddRangeAsync(midListAdd, cancellationToken);
}
break;
case Internal.Model.TableRefType.OneToMany:
@ -318,7 +319,7 @@ namespace FreeSql
var val = FreeSql.Internal.Utils.GetDataReaderValue(tref.RefColumns[colidx].CsType, _db.OrmOriginal.GetEntityValueWithPropertyName(_table.Type, item, tref.Columns[colidx].CsName));
_db.OrmOriginal.SetEntityValueWithPropertyName(tref.RefEntityType, propValItem, tref.RefColumns[colidx].CsName, val);
}
await refSet.AddOrUpdateAsync(propValItem);
await refSet.AddOrUpdateAsync(propValItem, cancellationToken);
}
break;
}
@ -333,9 +334,9 @@ namespace FreeSql
#endregion
#region UpdateAsync
Task<int> DbContextBatchUpdateAsync(EntityState[] ups) => DbContextBatchUpdatePrivAsync(ups, false);
Task<int> DbContextBatchUpdateNowAsync(EntityState[] ups) => DbContextBatchUpdatePrivAsync(ups, true);
async Task<int> DbContextBatchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate)
Task<int> DbContextBatchUpdateAsync(EntityState[] ups, CancellationToken cancellationToken) => DbContextBatchUpdatePrivAsync(ups, false, cancellationToken);
Task<int> DbContextBatchUpdateNowAsync(EntityState[] ups, CancellationToken cancellationToken) => DbContextBatchUpdatePrivAsync(ups, true, cancellationToken);
async Task<int> DbContextBatchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate, CancellationToken cancellationToken)
{
if (ups.Any() == false) return 0;
var uplst1 = ups[ups.Length - 1];
@ -370,7 +371,7 @@ namespace FreeSql
return ups.Length == data.Count ? -998 : -997;
var update = this.OrmUpdate(null).SetSource(data.Select(a => a.Value)).IgnoreColumns(cuig);
var affrows = await update.ExecuteAffrowsAsync();
var affrows = await update.ExecuteAffrowsAsync(cancellationToken);
_db._entityChangeReport.AddRange(data.Select(a => new DbContext.EntityChangeReport.ChangeInfo {
Object = a.Value,
BeforeObject = _states.TryGetValue(a.Key, out var beforeVal) ? CreateEntityState(beforeVal.Value).Value : null,
@ -390,20 +391,20 @@ namespace FreeSql
//等待下次对比再保存
return 0;
}
async public Task UpdateAsync(TEntity data)
async public Task UpdateAsync(TEntity data, CancellationToken cancellationToken = default)
{
var exists = ExistsInStates(data);
if (exists == null) throw new Exception($"不可更新,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
if (exists == false)
{
var olddata = await OrmSelect(data).FirstAsync();
var olddata = await OrmSelect(data).FirstAsync(cancellationToken);
if (olddata == null) throw new Exception($"不可更新,数据库不存在该记录:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
}
await UpdateRangePrivAsync(new[] { data }, true);
await UpdateRangePrivAsync(new[] { data }, true, cancellationToken);
}
public Task UpdateRangeAsync(IEnumerable<TEntity> data) => UpdateRangePrivAsync(data, true);
async Task UpdateRangePrivAsync(IEnumerable<TEntity> data, bool isCheck)
public Task UpdateRangeAsync(IEnumerable<TEntity> data, CancellationToken cancellationToken = default) => UpdateRangePrivAsync(data, true, cancellationToken);
async Task UpdateRangePrivAsync(IEnumerable<TEntity> data, bool isCheck, CancellationToken cancellationToken)
{
if (CanUpdate(data, true) == false) return;
foreach (var item in data)
@ -411,7 +412,7 @@ namespace FreeSql
if (_dicUpdateTimes.ContainsKey(item))
{
var itemCopy = CreateEntityState(item).Value;
await DbContextFlushCommandAsync();
await DbContextFlushCommandAsync(cancellationToken);
if (_table.VersionColumn != null)
{
var itemVersion = _db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, item, _table.VersionColumn.CsName);
@ -429,32 +430,27 @@ namespace FreeSql
}
if (_db.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
await AddOrUpdateNavigateListAsync(item, false);
await AddOrUpdateNavigateListAsync(item, false, null, cancellationToken);
}
#endregion
#region RemoveAsync
async Task<int> DbContextBatchRemoveAsync(EntityState[] dels)
async Task<int> DbContextBatchRemoveAsync(EntityState[] dels, CancellationToken cancellationToken)
{
if (dels.Any() == false) return 0;
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync(cancellationToken);
_db._entityChangeReport.AddRange(dels.Select(a => new DbContext.EntityChangeReport.ChangeInfo { Object = a.Value, Type = DbContext.EntityChangeType.Delete }));
return affrows;
}
/// <summary>
/// 根据 lambda 条件删除数据
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
async public Task<int> RemoveAsync(Expression<Func<TEntity, bool>> predicate)
async public Task<int> RemoveAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
await DbContextFlushCommandAsync();
return await this.OrmDelete(null).Where(predicate).ExecuteAffrowsAsync();
await DbContextFlushCommandAsync(cancellationToken);
return await this.OrmDelete(null).Where(predicate).ExecuteAffrowsAsync(cancellationToken);
}
#endregion
#region AddOrUpdateAsync
async public Task AddOrUpdateAsync(TEntity data)
async public Task AddOrUpdateAsync(TEntity data, CancellationToken cancellationToken = default)
{
if (data == null) throw new ArgumentNullException(nameof(data));
if (_table.Primarys.Any() == false) throw new Exception($"不可添加,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, data)}");
@ -462,23 +458,23 @@ namespace FreeSql
var flagExists = ExistsInStates(data);
if (flagExists == false)
{
var olddata = await OrmSelect(data).FirstAsync();
var olddata = await OrmSelect(data).FirstAsync(cancellationToken);
flagExists = olddata != null;
}
if (flagExists == true && CanUpdate(data, false))
{
await DbContextFlushCommandAsync();
await DbContextFlushCommandAsync(cancellationToken);
var affrows = _db._affrows;
await UpdateRangePrivAsync(new[] { data }, false);
await DbContextFlushCommandAsync();
await UpdateRangePrivAsync(new[] { data }, false, cancellationToken);
await DbContextFlushCommandAsync(cancellationToken);
affrows = _db._affrows - affrows;
if (affrows > 0) return;
}
if (CanAdd(data, false))
{
_db.OrmOriginal.ClearEntityPrimaryValueWithIdentity(_entityType, data);
await AddPrivAsync(data, false);
await AddPrivAsync(data, false, cancellationToken);
}
}
#endregion

View File

@ -54,7 +54,7 @@ namespace FreeSql
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data, true);
AddOrUpdateNavigateList(data, true, null);
}
else
{
@ -65,7 +65,7 @@ namespace FreeSql
_db.OrmOriginal.MapEntityValue(_entityType, newval, data);
Attach(newval);
if (_db.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data, true);
AddOrUpdateNavigateList(data, true, null);
}
return;
default:
@ -78,7 +78,7 @@ namespace FreeSql
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data, true);
AddOrUpdateNavigateList(data, true, null);
}
return;
}
@ -86,7 +86,7 @@ namespace FreeSql
EnqueueToDbContext(DbContext.EntityChangeType.Insert, CreateEntityState(data));
Attach(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
AddOrUpdateNavigateList(data, true);
AddOrUpdateNavigateList(data, true, null);
}
/// <summary>
/// 添加
@ -124,7 +124,7 @@ namespace FreeSql
AttachRange(rets);
if (_db.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
AddOrUpdateNavigateList(item, true);
AddOrUpdateNavigateList(item, true, null);
return;
default:
foreach (var s in data)
@ -140,7 +140,7 @@ namespace FreeSql
AttachRange(data);
if (_db.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
AddOrUpdateNavigateList(item, true);
AddOrUpdateNavigateList(item, true, null);
}
}
@ -210,7 +210,7 @@ namespace FreeSql
_db.Options.EnableAddOrUpdateNavigateList = oldEnable;
}
}
void AddOrUpdateNavigateList(TEntity item, bool isAdd, string propertyName = null)
void AddOrUpdateNavigateList(TEntity item, bool isAdd, string propertyName)
{
Action<PropertyInfo> action = prop =>
{
@ -472,7 +472,7 @@ namespace FreeSql
}
if (_db.Options.EnableAddOrUpdateNavigateList)
foreach (var item in data)
AddOrUpdateNavigateList(item, false);
AddOrUpdateNavigateList(item, false, null);
}
#endregion

View File

@ -130,13 +130,6 @@
清空状态数据
</summary>
</member>
<member name="M:FreeSql.DbSet`1.RemoveAsync(System.Linq.Expressions.Expression{System.Func{`0,System.Boolean}})">
<summary>
根据 lambda 条件删除数据
</summary>
<param name="predicate"></param>
<returns></returns>
</member>
<member name="M:FreeSql.DbSet`1.Add(`0)">
<summary>
添加
@ -509,14 +502,5 @@
<param name="that"></param>
<returns></returns>
</member>
<member name="M:Microsoft.Extensions.DependencyInjection.FreeSqlRepositoryDependencyInjection.AddFreeRepository(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{FreeSql.FluentDataFilter},System.Reflection.Assembly[])">
<summary>
批量注入 Repository可以参考代码自行调整
</summary>
<param name="services"></param>
<param name="globalDataFilter"></param>
<param name="assemblies"></param>
<returns></returns>
</member>
</members>
</doc>

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace FreeSql
@ -81,9 +82,9 @@ namespace FreeSql
}
#if net40
#else
async public override Task<int> SaveChangesAsync()
async public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
await FlushCommandAsync();
await FlushCommandAsync(cancellationToken);
return SaveChangesSuccess();
}
#endif

View File

@ -189,7 +189,7 @@ namespace FreeSql
}
public virtual int Delete(TKey id) => Delete(CheckTKeyAndReturnIdEntity(id));
public virtual TEntity Find(TKey id) => Select.WhereDynamic(CheckTKeyAndReturnIdEntity(id)).ToOne();
public TEntity Get(TKey id) => Find(id);
public virtual TEntity Find(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne();
public TEntity Get(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOne();
}
}

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
#if net40
@ -13,68 +14,68 @@ namespace FreeSql
where TEntity : class
{
async virtual public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate)
async virtual public Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
var delete = _dbset.OrmDeleteInternal(null).Where(predicate);
var sql = delete.ToSql();
var affrows = await delete.ExecuteAffrowsAsync();
var affrows = await delete.ExecuteAffrowsAsync(cancellationToken);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { Object = sql, Type = DbContext.EntityChangeType.SqlRaw });
return affrows;
}
public virtual Task<int> DeleteAsync(TEntity entity)
public virtual Task<int> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
{
_dbset.Remove(entity);
return _db.SaveChangesAsync();
return _db.SaveChangesAsync(cancellationToken);
}
public virtual Task<int> DeleteAsync(IEnumerable<TEntity> entitys)
public virtual Task<int> DeleteAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default)
{
_dbset.RemoveRange(entitys);
return _db.SaveChangesAsync();
return _db.SaveChangesAsync(cancellationToken);
}
async public virtual Task<TEntity> InsertAsync(TEntity entity)
async public virtual Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await _dbset.AddAsync(entity);
await _db.SaveChangesAsync();
await _dbset.AddAsync(entity, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
return entity;
}
async public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys)
async public virtual Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default)
{
await _dbset.AddRangeAsync(entitys);
await _db.SaveChangesAsync();
await _dbset.AddRangeAsync(entitys, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
return entitys.ToList();
}
public virtual Task<int> UpdateAsync(TEntity entity)
public virtual Task<int> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
{
_dbset.Update(entity);
return _db.SaveChangesAsync();
return _db.SaveChangesAsync(cancellationToken);
}
public virtual Task<int> UpdateAsync(IEnumerable<TEntity> entitys)
public virtual Task<int> UpdateAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default)
{
_dbset.UpdateRange(entitys);
return _db.SaveChangesAsync();
return _db.SaveChangesAsync(cancellationToken);
}
async public virtual Task<TEntity> InsertOrUpdateAsync(TEntity entity)
async public virtual Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
{
await _dbset.AddOrUpdateAsync(entity);
await _db.SaveChangesAsync();
await _dbset.AddOrUpdateAsync(entity, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
return entity;
}
async public Task SaveManyAsync(TEntity entity, string propertyName)
async public Task SaveManyAsync(TEntity entity, string propertyName, CancellationToken cancellationToken = default)
{
await _dbset.SaveManyAsync(entity, propertyName);
await _db.SaveChangesAsync();
await _dbset.SaveManyAsync(entity, propertyName, cancellationToken);
await _db.SaveChangesAsync(cancellationToken);
}
}
partial class BaseRepository<TEntity, TKey>
{
public virtual Task<int> DeleteAsync(TKey id) => DeleteAsync(CheckTKeyAndReturnIdEntity(id));
public virtual Task<TEntity> FindAsync(TKey id) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync();
public Task<TEntity> GetAsync(TKey id) => FindAsync(id);
public virtual Task<int> DeleteAsync(TKey id, CancellationToken cancellationToken = default) => DeleteAsync(CheckTKeyAndReturnIdEntity(id), cancellationToken);
public virtual Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
public Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default) => _dbset.OrmSelectInternal(CheckTKeyAndReturnIdEntity(id)).ToOneAsync(cancellationToken);
}
}
#endif

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace FreeSql
@ -98,17 +99,17 @@ namespace FreeSql
#if net40
#else
Task<TEntity> InsertAsync(TEntity entity);
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys);
Task<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default);
Task<List<TEntity>> InsertAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default);
Task<int> UpdateAsync(TEntity entity);
Task<int> UpdateAsync(IEnumerable<TEntity> entitys);
Task<TEntity> InsertOrUpdateAsync(TEntity entity);
Task SaveManyAsync(TEntity entity, string propertyName);
Task<int> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default);
Task<int> UpdateAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default);
Task<TEntity> InsertOrUpdateAsync(TEntity entity, CancellationToken cancellationToken = default);
Task SaveManyAsync(TEntity entity, string propertyName, CancellationToken cancellationToken = default);
Task<int> DeleteAsync(TEntity entity);
Task<int> DeleteAsync(IEnumerable<TEntity> entitys);
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate);
Task<int> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default);
Task<int> DeleteAsync(IEnumerable<TEntity> entitys, CancellationToken cancellationToken = default);
Task<int> DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
#endif
}
@ -121,9 +122,9 @@ namespace FreeSql
#if net40
#else
Task<TEntity> GetAsync(TKey id);
Task<TEntity> FindAsync(TKey id);
Task<int> DeleteAsync(TKey id);
Task<TEntity> GetAsync(TKey id, CancellationToken cancellationToken = default);
Task<TEntity> FindAsync(TKey id, CancellationToken cancellationToken = default);
Task<int> DeleteAsync(TKey id, CancellationToken cancellationToken = default);
#endif
}
}