mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-18 03:53:21 +08:00
- 增加 DbContext/Repository 比较变化方法 CompareState;
This commit is contained in:
parent
f007b3fa0e
commit
352ceceb23
@ -212,6 +212,17 @@ namespace FreeSql
|
|||||||
this.Set<TEntity>().AttachOnlyPrimary(data);
|
this.Set<TEntity>().AttachOnlyPrimary(data);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 比较实体,计算出值发生变化的属性,以及属性变化的前后值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Dictionary<string, object[]> CompareState<TEntity>(TEntity newdata) where TEntity : class
|
||||||
|
{
|
||||||
|
CheckEntityTypeOrThrow(typeof(TEntity));
|
||||||
|
return this.Set<TEntity>().CompareState(newdata);
|
||||||
|
}
|
||||||
#if net40
|
#if net40
|
||||||
#else
|
#else
|
||||||
public Task AddAsync<TEntity>(TEntity data) where TEntity : class
|
public Task AddAsync<TEntity>(TEntity data) where TEntity : class
|
||||||
|
@ -219,6 +219,31 @@ namespace FreeSql
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 比较实体,计算出值发生变化的属性,以及属性变化的前后值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public Dictionary<string, object[]> CompareState(TEntity newdata)
|
||||||
|
{
|
||||||
|
if (newdata == null) return null;
|
||||||
|
if (_table.Primarys.Any() == false) throw new Exception($"不可比较,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, newdata)}");
|
||||||
|
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, newdata, false);
|
||||||
|
if (string.IsNullOrEmpty(key)) throw new Exception($"不可比较,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, newdata)}");
|
||||||
|
if (_states.TryGetValue(key, out var oldState) == false || oldState == null)
|
||||||
|
return _table.ColumnsByCs.ToDictionary(a => a.Key, a => new object[]
|
||||||
|
{
|
||||||
|
_db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, newdata, a.Key),
|
||||||
|
null
|
||||||
|
});
|
||||||
|
|
||||||
|
return _db.OrmOriginal.CompareEntityValueReturnColumns(_entityType, oldState.Value, newdata, false).ToDictionary(a => a, a => new object[]
|
||||||
|
{
|
||||||
|
_db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, newdata, a),
|
||||||
|
_db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, oldState.Value, a)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 清空状态数据
|
/// 清空状态数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -62,6 +62,13 @@
|
|||||||
<typeparam name="TEntity"></typeparam>
|
<typeparam name="TEntity"></typeparam>
|
||||||
<param name="data"></param>
|
<param name="data"></param>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.DbContext.CompareState``1(``0)">
|
||||||
|
<summary>
|
||||||
|
比较实体,计算出值发生变化的属性,以及属性变化的前后值
|
||||||
|
</summary>
|
||||||
|
<param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="P:FreeSql.DbContext.EntityChangeReport.ChangeInfo.BeforeObject">
|
<member name="P:FreeSql.DbContext.EntityChangeReport.ChangeInfo.BeforeObject">
|
||||||
<summary>
|
<summary>
|
||||||
Type = Update 的时候,获取更新之前的对象
|
Type = Update 的时候,获取更新之前的对象
|
||||||
@ -130,6 +137,13 @@
|
|||||||
</summary>
|
</summary>
|
||||||
<param name="data"></param>
|
<param name="data"></param>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.DbSet`1.CompareState(`0)">
|
||||||
|
<summary>
|
||||||
|
比较实体,计算出值发生变化的属性,以及属性变化的前后值
|
||||||
|
</summary>
|
||||||
|
<param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.DbSet`1.FlushState">
|
<member name="M:FreeSql.DbSet`1.FlushState">
|
||||||
<summary>
|
<summary>
|
||||||
清空状态数据
|
清空状态数据
|
||||||
@ -314,6 +328,13 @@
|
|||||||
</summary>
|
</summary>
|
||||||
<param name="data"></param>
|
<param name="data"></param>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:FreeSql.IBaseRepository`1.CompareState(`0)">
|
||||||
|
<summary>
|
||||||
|
比较实体,计算出值发生变化的属性,以及属性变化的前后值
|
||||||
|
</summary>
|
||||||
|
<param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="M:FreeSql.IBaseRepository`1.SaveMany(`0,System.String)">
|
<member name="M:FreeSql.IBaseRepository`1.SaveMany(`0,System.String)">
|
||||||
<summary>
|
<summary>
|
||||||
保存实体的指定 ManyToMany/OneToMany 导航属性(完整对比)<para></para>
|
保存实体的指定 ManyToMany/OneToMany 导航属性(完整对比)<para></para>
|
||||||
|
@ -127,6 +127,7 @@ namespace FreeSql
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
public void FlushState() => _dbset.FlushState();
|
public void FlushState() => _dbset.FlushState();
|
||||||
|
public Dictionary<string, object[]> CompareState(TEntity newdata) => _dbset.CompareState(newdata);
|
||||||
|
|
||||||
public virtual TEntity InsertOrUpdate(TEntity entity)
|
public virtual TEntity InsertOrUpdate(TEntity entity)
|
||||||
{
|
{
|
||||||
|
@ -58,6 +58,12 @@ namespace FreeSql
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
IBaseRepository<TEntity> AttachOnlyPrimary(TEntity data);
|
IBaseRepository<TEntity> AttachOnlyPrimary(TEntity data);
|
||||||
|
/// <summary>
|
||||||
|
/// 比较实体,计算出值发生变化的属性,以及属性变化的前后值
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
Dictionary<string, object[]> CompareState(TEntity newdata);
|
||||||
|
|
||||||
int Update(TEntity entity);
|
int Update(TEntity entity);
|
||||||
int Update(IEnumerable<TEntity> entitys);
|
int Update(IEnumerable<TEntity> entitys);
|
||||||
|
@ -28,6 +28,7 @@ namespace FreeSql.Tests
|
|||||||
items[0].Title = "88";
|
items[0].Title = "88";
|
||||||
//items[1].Title = "88";
|
//items[1].Title = "88";
|
||||||
items[2].Title = "88";
|
items[2].Title = "88";
|
||||||
|
var changed = repos.CompareState(items[0]);
|
||||||
int x = await repos.UpdateAsync(items);
|
int x = await repos.UpdateAsync(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user