mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 20:08:15 +08:00
移除 FreeSql.DbContext,成为独立项目
This commit is contained in:
@ -1,215 +0,0 @@
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using FreeSql.Internal.Model;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace FreeSql {
|
||||
|
||||
internal class DbContextDbSet<TEntity> : DbSet<TEntity> where TEntity : class {
|
||||
|
||||
public DbContextDbSet(DbContext ctx) {
|
||||
_ctx = ctx;
|
||||
_uow = ctx._uow;
|
||||
_fsql = ctx._fsql;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract partial class DbSet<TEntity> : IDisposable where TEntity : class {
|
||||
|
||||
internal DbContext _ctx;
|
||||
internal IUnitOfWork _uow;
|
||||
internal IFreeSql _fsql;
|
||||
|
||||
protected virtual ISelect<TEntity> OrmSelect(object dywhere) {
|
||||
DbContextExecCommand(); //查询前先提交,否则会出脏读
|
||||
return _fsql.Select<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction(false)).TrackToList(TrackToList);
|
||||
}
|
||||
|
||||
~DbSet() {
|
||||
this.Dispose();
|
||||
}
|
||||
bool _isdisposed = false;
|
||||
public void Dispose() {
|
||||
if (_isdisposed) return;
|
||||
try {
|
||||
this._dicUpdateTimes.Clear();
|
||||
this._states.Clear();
|
||||
} finally {
|
||||
_isdisposed = true;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual IInsert<TEntity> OrmInsert() => _fsql.Insert<TEntity>().WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
protected virtual IInsert<TEntity> OrmInsert(TEntity data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> data) => _fsql.Insert<TEntity>(data).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
|
||||
protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys) => _fsql.Update<TEntity>().SetSource(entitys).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
protected virtual IDelete<TEntity> OrmDelete(object dywhere) => _fsql.Delete<TEntity>(dywhere).WithTransaction(_uow?.GetOrBeginTransaction());
|
||||
|
||||
internal void EnqueueToDbContext(DbContext.ExecCommandInfoType actionType, EntityState state) {
|
||||
_ctx.EnqueueAction(actionType, this, typeof(EntityState), state);
|
||||
}
|
||||
internal void IncrAffrows(int affrows) {
|
||||
_ctx._affrows += affrows;
|
||||
}
|
||||
internal void TrackToList(object list) {
|
||||
if (list == null) return;
|
||||
var ls = list as IList<TEntity>;
|
||||
if (ls == null) return;
|
||||
|
||||
foreach (var item in ls) {
|
||||
var key = _fsql.GetEntityKeyString(item);
|
||||
if (_states.ContainsKey(key)) {
|
||||
_fsql.MapEntityValue(item, _states[key].Value);
|
||||
_states[key].Time = DateTime.Now;
|
||||
} else {
|
||||
_states.Add(key, CreateEntityState(item));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ISelect<TEntity> Select => this.OrmSelect(null);
|
||||
public ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).Where(exp);
|
||||
public ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => this.OrmSelect(null).WhereIf(condition, exp);
|
||||
|
||||
protected Dictionary<string, EntityState> _states = new Dictionary<string, EntityState>();
|
||||
internal Dictionary<string, EntityState> _statesInternal => _states;
|
||||
TableInfo _tablePriv;
|
||||
protected TableInfo _table => _tablePriv ?? (_tablePriv = _fsql.CodeFirst.GetTableByEntity(_entityType));
|
||||
ColumnInfo[] _tableIdentitysPriv;
|
||||
protected ColumnInfo[] _tableIdentitys => _tableIdentitysPriv ?? (_tableIdentitysPriv = _table.Primarys.Where(a => a.Attribute.IsIdentity).ToArray());
|
||||
protected Type _entityType = typeof(TEntity);
|
||||
internal Type _entityTypeInternal => _entityType;
|
||||
|
||||
public class EntityState {
|
||||
public EntityState(TEntity value, string key) {
|
||||
this.Value = value;
|
||||
this.Key = key;
|
||||
this.Time = DateTime.Now;
|
||||
}
|
||||
public TEntity OldValue { get; set; }
|
||||
public TEntity Value { get; set; }
|
||||
public string Key { get; set; }
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
|
||||
#region Utils
|
||||
EntityState CreateEntityState(TEntity data) {
|
||||
if (data == null) throw new ArgumentNullException(nameof(data));
|
||||
var key = _fsql.GetEntityKeyString(data);
|
||||
var state = new EntityState(Activator.CreateInstance<TEntity>(), key);
|
||||
_fsql.MapEntityValue(data, state.Value);
|
||||
return state;
|
||||
}
|
||||
bool ExistsInStates(TEntity data) {
|
||||
if (data == null) throw new ArgumentNullException(nameof(data));
|
||||
var key = _fsql.GetEntityKeyString(data);
|
||||
if (string.IsNullOrEmpty(key)) return false;
|
||||
return _states.ContainsKey(key);
|
||||
}
|
||||
|
||||
bool CanAdd(IEnumerable<TEntity> data, bool isThrow) {
|
||||
if (data == null) {
|
||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||
return false;
|
||||
}
|
||||
foreach (var s in data) if (CanAdd(s, isThrow) == false) return false;
|
||||
return true;
|
||||
}
|
||||
bool CanAdd(TEntity data, bool isThrow) {
|
||||
if (data == null) {
|
||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||
return false;
|
||||
}
|
||||
var key = _fsql.GetEntityKeyString(data);
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
return true;
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||
return true;
|
||||
}
|
||||
if (isThrow) throw new Exception($"不可添加,未设置主键的值:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (_states.ContainsKey(key)) {
|
||||
if (isThrow) throw new Exception($"不可添加,已存在于状态管理:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
var idval = _fsql.GetEntityIdentityValueWithPrimary(data);
|
||||
if (idval > 0) {
|
||||
if (isThrow) throw new Exception($"不可添加,自增属性有值:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CanUpdate(IEnumerable<TEntity> data, bool isThrow) {
|
||||
if (data == null) {
|
||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||
return false;
|
||||
}
|
||||
foreach (var s in data) if (CanUpdate(s, isThrow) == false) return false;
|
||||
return true;
|
||||
}
|
||||
bool CanUpdate(TEntity data, bool isThrow) {
|
||||
if (data == null) {
|
||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||
return false;
|
||||
}
|
||||
if (_table.Primarys.Any() == false) {
|
||||
if (isThrow) throw new Exception($"不可更新,实体没有主键:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
var key = _fsql.GetEntityKeyString(data);
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
if (isThrow) throw new Exception($"不可更新,未设置主键的值:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
if (_states.TryGetValue(key, out var tryval) == false) {
|
||||
if (isThrow) throw new Exception($"不可更新,数据未被跟踪,应该先查询:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CanRemove(IEnumerable<TEntity> data, bool isThrow) {
|
||||
if (data == null) {
|
||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||
return false;
|
||||
}
|
||||
foreach (var s in data) if (CanRemove(s, isThrow) == false) return false;
|
||||
return true;
|
||||
}
|
||||
bool CanRemove(TEntity data, bool isThrow) {
|
||||
if (data == null) {
|
||||
if (isThrow) throw new ArgumentNullException(nameof(data));
|
||||
return false;
|
||||
}
|
||||
if (_table.Primarys.Any() == false) {
|
||||
if (isThrow) throw new Exception($"不可删除,实体没有主键:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
var key = _fsql.GetEntityKeyString(data);
|
||||
if (string.IsNullOrEmpty(key)) {
|
||||
if (isThrow) throw new Exception($"不可删除,未设置主键的值:{_fsql.GetEntityString(data)}");
|
||||
return false;
|
||||
}
|
||||
//if (_states.TryGetValue(key, out var tryval) == false) {
|
||||
// if (isThrow) throw new Exception($"不可删除,数据未被跟踪,应该先查询:{_fsql.GetEntityString(data)}");
|
||||
// return false;
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,158 +0,0 @@
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FreeSql {
|
||||
partial class DbSet<TEntity> {
|
||||
|
||||
Task DbContextExecCommandAsync() {
|
||||
_dicUpdateTimes.Clear();
|
||||
return _ctx.ExecCommandAsync();
|
||||
}
|
||||
|
||||
async Task<int> DbContextBetchAddAsync(EntityState[] adds) {
|
||||
if (adds.Any() == false) return 0;
|
||||
var affrows = await this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrowsAsync();
|
||||
return affrows;
|
||||
}
|
||||
|
||||
#region Add
|
||||
async Task AddPrivAsync(TEntity data, bool isCheck) {
|
||||
if (isCheck && CanAdd(data, true) == false) return;
|
||||
if (_tableIdentitys.Length > 0) {
|
||||
//有自增,马上执行
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||
await DbContextExecCommandAsync();
|
||||
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
|
||||
IncrAffrows(1);
|
||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||
var state = CreateEntityState(data);
|
||||
_states.Add(state.Key, state);
|
||||
} else {
|
||||
await DbContextExecCommandAsync();
|
||||
var newval = (await this.OrmInsert(data).ExecuteInsertedAsync()).First();
|
||||
IncrAffrows(1);
|
||||
_fsql.MapEntityValue(newval, data);
|
||||
var state = CreateEntityState(newval);
|
||||
_states.Add(state.Key, state);
|
||||
}
|
||||
return;
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||
await DbContextExecCommandAsync();
|
||||
var idtval = await this.OrmInsert(data).ExecuteIdentityAsync();
|
||||
IncrAffrows(1);
|
||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||
var state = CreateEntityState(data);
|
||||
_states.Add(state.Key, state);
|
||||
}
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||
}
|
||||
}
|
||||
public Task AddAsync(TEntity data) => AddPrivAsync(data, true);
|
||||
async public Task AddRangeAsync(IEnumerable<TEntity> data) {
|
||||
if (CanAdd(data, true) == false) return;
|
||||
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
||||
await AddAsync(data.First());
|
||||
return;
|
||||
}
|
||||
if (_tableIdentitys.Length > 0) {
|
||||
//有自增,马上执行
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
await DbContextExecCommandAsync();
|
||||
var rets = await this.OrmInsert(data).ExecuteInsertedAsync();
|
||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||
var idx = 0;
|
||||
foreach (var s in data)
|
||||
_fsql.MapEntityValue(rets[idx++], s);
|
||||
IncrAffrows(rets.Count);
|
||||
TrackToList(rets);
|
||||
return;
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
foreach (var s in data)
|
||||
await AddPrivAsync(s, false);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var item in data)
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(item));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region UpdateAsync
|
||||
Task<int> DbContextBetchUpdateAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, false);
|
||||
Task<int> DbContextBetchUpdateNowAsync(EntityState[] ups) => DbContextBetchUpdatePrivAsync(ups, true);
|
||||
async Task<int> DbContextBetchUpdatePrivAsync(EntityState[] ups, bool isLiveUpdate) {
|
||||
if (ups.Any() == false) return 0;
|
||||
var uplst1 = ups[ups.Length - 1];
|
||||
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
||||
|
||||
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
||||
var lstval2 = default(EntityState);
|
||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_fsql.GetEntityString(uplst2.Value)}");
|
||||
|
||||
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
|
||||
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
|
||||
|
||||
List<EntityState> data = null;
|
||||
string[] cuig = null;
|
||||
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
|
||||
//最后一个不保存
|
||||
data = ups.ToList();
|
||||
data.RemoveAt(ups.Length - 1);
|
||||
cuig = cuig2;
|
||||
} else if (isLiveUpdate) {
|
||||
//立即保存
|
||||
data = ups.ToList();
|
||||
cuig = cuig1;
|
||||
}
|
||||
|
||||
if (data?.Count > 0) {
|
||||
|
||||
if (cuig.Length == _table.Columns.Count)
|
||||
return ups.Length == data.Count ? -998 : -997;
|
||||
|
||||
var updateSource = data.Select(a => a.Value).ToArray();
|
||||
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
|
||||
|
||||
var affrows = await update.ExecuteAffrowsAsync();
|
||||
|
||||
foreach (var newval in data) {
|
||||
if (_states.TryGetValue(newval.Key, out var tryold))
|
||||
_fsql.MapEntityValue(newval.Value, tryold.Value);
|
||||
if (newval.OldValue != null)
|
||||
_fsql.MapEntityValue(newval.Value, newval.OldValue);
|
||||
}
|
||||
return affrows;
|
||||
}
|
||||
|
||||
//等待下次对比再保存
|
||||
return 0;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region RemoveAsync
|
||||
async Task<int> DbContextBetchRemoveAsync(EntityState[] dels) {
|
||||
if (dels.Any() == false) return 0;
|
||||
var affrows = await this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrowsAsync();
|
||||
return Math.Max(dels.Length, affrows);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -1,183 +0,0 @@
|
||||
using FreeSql.Extensions.EntityUtil;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace FreeSql {
|
||||
partial class DbSet<TEntity> {
|
||||
|
||||
void DbContextExecCommand() {
|
||||
_dicUpdateTimes.Clear();
|
||||
_ctx.ExecCommand();
|
||||
}
|
||||
|
||||
int DbContextBetchAdd(EntityState[] adds) {
|
||||
if (adds.Any() == false) return 0;
|
||||
var affrows = this.OrmInsert(adds.Select(a => a.Value)).ExecuteAffrows();
|
||||
return affrows;
|
||||
}
|
||||
|
||||
#region Add
|
||||
void AddPriv(TEntity data, bool isCheck) {
|
||||
if (isCheck && CanAdd(data, true) == false) return;
|
||||
if (_tableIdentitys.Length > 0) {
|
||||
//有自增,马上执行
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||
DbContextExecCommand();
|
||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
||||
IncrAffrows(1);
|
||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||
var state = CreateEntityState(data);
|
||||
_states.Add(state.Key, state);
|
||||
} else {
|
||||
DbContextExecCommand();
|
||||
var newval = this.OrmInsert(data).ExecuteInserted().First();
|
||||
IncrAffrows(1);
|
||||
_fsql.MapEntityValue(newval, data);
|
||||
var state = CreateEntityState(newval);
|
||||
_states.Add(state.Key, state);
|
||||
}
|
||||
return;
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
if (_tableIdentitys.Length == 1 && _table.Primarys.Length == 1) {
|
||||
DbContextExecCommand();
|
||||
var idtval = this.OrmInsert(data).ExecuteIdentity();
|
||||
IncrAffrows(1);
|
||||
_fsql.SetEntityIdentityValueWithPrimary(data, idtval);
|
||||
var state = CreateEntityState(data);
|
||||
_states.Add(state.Key, state);
|
||||
}
|
||||
return;
|
||||
}
|
||||
} else
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(data));
|
||||
}
|
||||
public void Add(TEntity data) => AddPriv(data, true);
|
||||
public void AddRange(IEnumerable<TEntity> data) {
|
||||
if (CanAdd(data, true) == false) return;
|
||||
if (data.ElementAtOrDefault(1) == default(TEntity)) {
|
||||
Add(data.First());
|
||||
return;
|
||||
}
|
||||
if (_tableIdentitys.Length > 0) {
|
||||
//有自增,马上执行
|
||||
switch (_fsql.Ado.DataType) {
|
||||
case DataType.SqlServer:
|
||||
case DataType.PostgreSQL:
|
||||
DbContextExecCommand();
|
||||
var rets = this.OrmInsert(data).ExecuteInserted();
|
||||
if (rets.Count != data.Count()) throw new Exception($"特别错误:批量添加失败,{_fsql.Ado.DataType} 的返回数据,与添加的数目不匹配");
|
||||
var idx = 0;
|
||||
foreach (var s in data)
|
||||
_fsql.MapEntityValue(rets[idx++], s);
|
||||
IncrAffrows(rets.Count);
|
||||
TrackToList(rets);
|
||||
return;
|
||||
case DataType.MySql:
|
||||
case DataType.Oracle:
|
||||
case DataType.Sqlite:
|
||||
foreach (var s in data)
|
||||
AddPriv(s, false);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
//进入队列,等待 SaveChanges 时执行
|
||||
foreach (var item in data)
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Insert, CreateEntityState(item));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
int DbContextBetchUpdate(EntityState[] ups) => DbContextBetchUpdatePriv(ups, false);
|
||||
int DbContextBetchUpdateNow(EntityState[] ups) => DbContextBetchUpdatePriv(ups, true);
|
||||
int DbContextBetchUpdatePriv(EntityState[] ups, bool isLiveUpdate) {
|
||||
if (ups.Any() == false) return 0;
|
||||
var uplst1 = ups[ups.Length - 1];
|
||||
var uplst2 = ups.Length > 1 ? ups[ups.Length - 2] : null;
|
||||
|
||||
if (_states.TryGetValue(uplst1.Key, out var lstval1) == false) return -999;
|
||||
var lstval2 = default(EntityState);
|
||||
if (uplst2 != null && _states.TryGetValue(uplst2.Key, out lstval2) == false) throw new Exception($"特别错误:更新失败,数据未被跟踪:{_fsql.GetEntityString(uplst2.Value)}");
|
||||
|
||||
var cuig1 = _fsql.CompareEntityValueReturnColumns(uplst1.Value, lstval1.Value, true);
|
||||
var cuig2 = uplst2 != null ? _fsql.CompareEntityValueReturnColumns(uplst2.Value, lstval2.Value, true) : null;
|
||||
|
||||
List<EntityState> data = null;
|
||||
string[] cuig = null;
|
||||
if (uplst2 != null && string.Compare(string.Join(",", cuig1), string.Join(",", cuig2)) != 0) {
|
||||
//最后一个不保存
|
||||
data = ups.ToList();
|
||||
data.RemoveAt(ups.Length - 1);
|
||||
cuig = cuig2;
|
||||
} else if (isLiveUpdate) {
|
||||
//立即保存
|
||||
data = ups.ToList();
|
||||
cuig = cuig1;
|
||||
}
|
||||
|
||||
if (data?.Count > 0) {
|
||||
|
||||
if (cuig.Length == _table.Columns.Count)
|
||||
return ups.Length == data.Count ? -998 : -997;
|
||||
|
||||
var updateSource = data.Select(a => a.Value).ToArray();
|
||||
var update = this.OrmUpdate(null).SetSource(updateSource).IgnoreColumns(cuig);
|
||||
|
||||
var affrows = update.ExecuteAffrows();
|
||||
|
||||
foreach (var newval in data) {
|
||||
if (_states.TryGetValue(newval.Key, out var tryold))
|
||||
_fsql.MapEntityValue(newval.Value, tryold.Value);
|
||||
if (newval.OldValue != null)
|
||||
_fsql.MapEntityValue(newval.Value, newval.OldValue);
|
||||
}
|
||||
return affrows;
|
||||
}
|
||||
|
||||
//等待下次对比再保存
|
||||
return 0;
|
||||
}
|
||||
|
||||
Dictionary<TEntity, byte> _dicUpdateTimes = new Dictionary<TEntity, byte>();
|
||||
public void Update(TEntity data) => UpdateRange(new[] { data });
|
||||
public void UpdateRange(IEnumerable<TEntity> data) {
|
||||
if (CanUpdate(data, true) == false) return;
|
||||
foreach (var item in data) {
|
||||
if (_dicUpdateTimes.ContainsKey(item))
|
||||
DbContextExecCommand();
|
||||
_dicUpdateTimes.Add(item, 1);
|
||||
|
||||
var state = CreateEntityState(item);
|
||||
state.OldValue = item;
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Update, state);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Remove
|
||||
int DbContextBetchRemove(EntityState[] dels) {
|
||||
if (dels.Any() == false) return 0;
|
||||
var affrows = this.OrmDelete(dels.Select(a => a.Value)).ExecuteAffrows();
|
||||
return Math.Max(dels.Length, affrows);
|
||||
}
|
||||
|
||||
public void Remove(TEntity data) => RemoveRange(new[] { data });
|
||||
public void RemoveRange(IEnumerable<TEntity> data) {
|
||||
if (CanRemove(data, true) == false) return;
|
||||
foreach (var item in data) {
|
||||
var state = CreateEntityState(item);
|
||||
if (_states.ContainsKey(state.Key)) _states.Remove(state.Key);
|
||||
_fsql.ClearEntityPrimaryValueWithIdentityAndGuid(item);
|
||||
|
||||
EnqueueToDbContext(DbContext.ExecCommandInfoType.Delete, state);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user