Skip to content

Commit

Permalink
- 修复 Repository/DbContext AuditValue 设置后无法从实体中获取该值;#1746
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Aug 9, 2024
1 parent d0317af commit 51409e7
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 35 deletions.
36 changes: 13 additions & 23 deletions FreeSql.DbContext/DbSet/DbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,13 @@ protected virtual IInsert<TEntity> OrmInsert()
protected virtual IInsert<TEntity> OrmInsert(TEntity entity)
{
var insert = OrmInsert();
if (entity != null)
{
(insert as InsertProvider<TEntity>)._source.Add(entity); //防止 Aop.AuditValue 触发两次
if (_db.Options.AuditValue != null)
_db.Options.AuditValue(new DbContextAuditValueEventArgs(Aop.AuditValueType.Insert, _table.Type, entity));
}
if (entity != null) (insert as InsertProvider<TEntity>)._source.Add(entity); //防止 Aop.AuditValue 触发两次
return insert;
}
protected virtual IInsert<TEntity> OrmInsert(IEnumerable<TEntity> entitys)
{
var insert = OrmInsert();
if (entitys != null)
{
(insert as InsertProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
if (_db.Options.AuditValue != null)
foreach (var item in entitys)
_db.Options.AuditValue(new DbContextAuditValueEventArgs(Aop.AuditValueType.Insert, _table.Type, item));
}
if (entitys != null) (insert as InsertProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
return insert;
}

Expand All @@ -95,13 +84,7 @@ protected virtual IUpdate<TEntity> OrmUpdate(IEnumerable<TEntity> entitys)
var update = _db.OrmOriginal.Update<TEntity>().AsType(_entityType).WithTransaction(_uow?.GetOrBeginTransaction());
if (_db.Options.NoneParameter != null) update.NoneParameter(_db.Options.NoneParameter.Value);
if (_db.Options.EnableGlobalFilter == false) update.DisableGlobalFilter();
if (entitys != null)
{
(update as UpdateProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
if (_db.Options.AuditValue != null)
foreach (var item in entitys)
_db.Options.AuditValue(new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, _table.Type, item));
}
if (entitys != null) (update as UpdateProvider<TEntity>)._source.AddRange(entitys.Where(a => a != null)); //防止 Aop.AuditValue 触发两次
return update;
}
protected virtual IDelete<TEntity> OrmDelete(object dywhere)
Expand Down Expand Up @@ -232,14 +215,19 @@ public EntityState(TEntity value, string key)
/// 附加实体,可用于不查询就更新或删除
/// </summary>
/// <param name="data"></param>
public void Attach(TEntity data) => AttachRange(new[] { data });
public void AttachRange(IEnumerable<TEntity> data)
public void Attach(TEntity data) => AttachPriv(new[] { data }, true);
public void AttachRange(IEnumerable<TEntity> data) => AttachPriv(data, true);
void AttachPriv(IEnumerable<TEntity> data, bool isAuditValue)
{
if (data == null || data.Any() == false) return;
if (_table.Primarys.Any() == false) throw new Exception(DbContextStrings.CannotAttach_EntityHasNo_PrimaryKey(_db.OrmOriginal.GetEntityString(_entityType, data.First())));
foreach (var item in data)
{
FreeSql.Internal.CommonProvider.UpdateProvider<TEntity>.AuditDataValue(this, item, _db.OrmOriginal, _table, null); //与 CanUpdate 同步
if (isAuditValue)
{
FreeSql.Internal.CommonProvider.UpdateProvider<TEntity>.AuditDataValue(this, item, _db.OrmOriginal, _table, null); //与 CanUpdate 同步
_db.Options.AuditValue?.Invoke(new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, _table.Type, item));
}
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, item, false);
if (string.IsNullOrEmpty(key)) throw new Exception(DbContextStrings.CannotAttach_PrimaryKey_NotSet(_db.OrmOriginal.GetEntityString(_entityType, item)));

Expand Down Expand Up @@ -342,6 +330,7 @@ bool CanAdd(TEntity data, bool isThrow)
return false;
}
FreeSql.Internal.CommonProvider.InsertProvider<TEntity>.AuditDataValue(this, data, _db.OrmOriginal, _table, null);
_db.Options.AuditValue?.Invoke(new DbContextAuditValueEventArgs(Aop.AuditValueType.Insert, _table.Type, data));
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, data, true);
if (string.IsNullOrEmpty(key))
{
Expand Down Expand Up @@ -407,6 +396,7 @@ bool CanUpdate(TEntity data, bool isThrow)
return false;
}
FreeSql.Internal.CommonProvider.UpdateProvider<TEntity>.AuditDataValue(this, data, _db.OrmOriginal, _table, null);
_db.Options.AuditValue?.Invoke(new DbContextAuditValueEventArgs(Aop.AuditValueType.Update, _table.Type, data));
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, data, false);
if (string.IsNullOrEmpty(key))
{
Expand Down
12 changes: 6 additions & 6 deletions FreeSql.DbContext/DbSet/DbSetAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
IncrAffrows(1);
_db.OrmOriginal.SetEntityValueWithPropertyName(_entityType, data, _tableIdentitys[0].CsName, idtval);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
AttachPriv(new[] { data }, false);
if (_db.Options.EnableCascadeSave)
await AddOrUpdateNavigateAsync(data, true, null, cancellationToken);
}
Expand All @@ -66,7 +66,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = newval, Type = DbContext.EntityChangeType.Insert });
IncrAffrows(1);
_db.OrmOriginal.MapEntityValue(_entityType, newval, data);
Attach(newval);
AttachPriv(new[] { newval }, false);
if (_db.Options.EnableCascadeSave)
await AddOrUpdateNavigateAsync(data, true, null, cancellationToken);
}
Expand All @@ -79,7 +79,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
IncrAffrows(1);
_db.OrmOriginal.SetEntityValueWithPropertyName(_entityType, data, _tableIdentitys[0].CsName, idtval);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
AttachPriv(new[] { data }, false);
if (_db.Options.EnableCascadeSave)
await AddOrUpdateNavigateAsync(data, true, null, cancellationToken);
return;
Expand All @@ -88,7 +88,7 @@ async Task AddPrivAsync(TEntity data, bool isCheck, CancellationToken cancellati
}
}
EnqueueToDbContext(DbContext.EntityChangeType.Insert, CreateEntityState(data));
Attach(data);
AttachPriv(new[] { data }, false);
if (_db.Options.EnableCascadeSave)
await AddOrUpdateNavigateAsync(data, true, null, cancellationToken);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ async public Task AddRangeAsync(IEnumerable<TEntity> data, CancellationToken can
foreach (var s in data)
_db.OrmOriginal.MapEntityValue(_entityType, rets[idx++], s);
IncrAffrows(rets.Count);
AttachRange(rets);
AttachPriv(rets, false);
if (_db.Options.EnableCascadeSave)
foreach (var item in data)
await AddOrUpdateNavigateAsync(item, true, null, cancellationToken);
Expand All @@ -141,7 +141,7 @@ async public Task AddRangeAsync(IEnumerable<TEntity> data, CancellationToken can
//进入队列,等待 SaveChanges 时执行
foreach (var item in data)
EnqueueToDbContext(DbContext.EntityChangeType.Insert, CreateEntityState(item));
AttachRange(data);
AttachPriv(data, false);
if (_db.Options.EnableCascadeSave)
foreach (var item in data)
await AddOrUpdateNavigateAsync(item, true, null, cancellationToken);
Expand Down
12 changes: 6 additions & 6 deletions FreeSql.DbContext/DbSet/DbSetSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void AddPriv(TEntity data, bool isCheck)
IncrAffrows(1);
_db.OrmOriginal.SetEntityValueWithPropertyName(_entityType, data, _tableIdentitys[0].CsName, idtval);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
AttachPriv(new[] { data }, false);
if (_db.Options.EnableCascadeSave)
AddOrUpdateNavigate(data, true, null);
}
Expand All @@ -65,7 +65,7 @@ void AddPriv(TEntity data, bool isCheck)
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = newval, Type = DbContext.EntityChangeType.Insert });
IncrAffrows(1);
_db.OrmOriginal.MapEntityValue(_entityType, newval, data);
Attach(newval);
AttachPriv(new[] { newval }, false);
if (_db.Options.EnableCascadeSave)
AddOrUpdateNavigate(data, true, null);
}
Expand All @@ -78,7 +78,7 @@ void AddPriv(TEntity data, bool isCheck)
IncrAffrows(1);
_db.OrmOriginal.SetEntityValueWithPropertyName(_entityType, data, _tableIdentitys[0].CsName, idtval);
_db._entityChangeReport.Add(new DbContext.EntityChangeReport.ChangeInfo { EntityType = _entityType, Object = data, Type = DbContext.EntityChangeType.Insert });
Attach(data);
AttachPriv(new[] { data }, false);
if (_db.Options.EnableCascadeSave)
AddOrUpdateNavigate(data, true, null);
return;
Expand All @@ -87,7 +87,7 @@ void AddPriv(TEntity data, bool isCheck)
}
}
EnqueueToDbContext(DbContext.EntityChangeType.Insert, CreateEntityState(data));
Attach(data);
AttachPriv(new[] { data }, false);
if (_db.Options.EnableCascadeSave)
AddOrUpdateNavigate(data, true, null);
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public void AddRange(IEnumerable<TEntity> data)
foreach (var s in data)
_db.OrmOriginal.MapEntityValue(_entityType, rets[idx++], s);
IncrAffrows(rets.Count);
AttachRange(rets);
AttachPriv(rets, false);
if (_db.Options.EnableCascadeSave)
foreach (var item in data)
AddOrUpdateNavigate(item, true, null);
Expand All @@ -144,7 +144,7 @@ public void AddRange(IEnumerable<TEntity> data)
//进入队列,等待 SaveChanges 时执行
foreach (var item in data)
EnqueueToDbContext(DbContext.EntityChangeType.Insert, CreateEntityState(item));
AttachRange(data);
AttachPriv(data, false);
if (_db.Options.EnableCascadeSave)
foreach (var item in data)
AddOrUpdateNavigate(item, true, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,51 @@ public OrderRepository(IFreeSql fsql, UnitOfWorkManager uowManager) : base(uowMa
public override ISelect<Order> Select => base.SelectDiy;
}

[Fact]
public void Test41()
{
using (var fsql = g.CreateMemory())
{
var repo = fsql.GetRepository<Test4Id>();
repo.DbContextOptions.AuditValue += e =>
{
if (e.Object is Test4Id item)
{
item.Id = new Random().Next();
}
};
var item = new Test4Id { Id = 0 };
repo.Insert(item);
Assert.True(item.Id > 0);

}
}
[Fact]
public void Test40()
{
using (var fsql = g.CreateMemory())
{
fsql.Aop.AuditValue += (s, e) =>
{
if (e.Object is Test4Id item)
{
item.Id = new Random().Next();
e.ObjectAuditBreak = true;
}
};
var repo = fsql.GetRepository<Test4Id>();
var item = new Test4Id { Id = 0 };
repo.Insert(item);
Assert.True(item.Id > 0);

}
}
class Test4Id
{
public int Id { get; set; }
public string Field2 { get; set; }
}

[Fact]
public void Test3()
{
Expand Down
109 changes: 109 additions & 0 deletions FreeSql/FreeSql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 51409e7

Please sign in to comment.