Skip to content

Commit 352cece

Browse files
committed
- 增加 DbContext/Repository 比较变化方法 CompareState;
1 parent f007b3f commit 352cece

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

FreeSql.DbContext/DbContext/DbContext.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ public DbContext AttachOnlyPrimary<TEntity>(TEntity data) where TEntity : class
212212
this.Set<TEntity>().AttachOnlyPrimary(data);
213213
return this;
214214
}
215+
216+
/// <summary>
217+
/// 比较实体,计算出值发生变化的属性,以及属性变化的前后值
218+
/// </summary>
219+
/// <param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
220+
/// <returns></returns>
221+
public Dictionary<string, object[]> CompareState<TEntity>(TEntity newdata) where TEntity : class
222+
{
223+
CheckEntityTypeOrThrow(typeof(TEntity));
224+
return this.Set<TEntity>().CompareState(newdata);
225+
}
215226
#if net40
216227
#else
217228
public Task AddAsync<TEntity>(TEntity data) where TEntity : class

FreeSql.DbContext/DbSet/DbSet.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,31 @@ public DbSet<TEntity> AttachOnlyPrimary(TEntity data)
219219
return this;
220220
}
221221

222+
/// <summary>
223+
/// 比较实体,计算出值发生变化的属性,以及属性变化的前后值
224+
/// </summary>
225+
/// <param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
226+
/// <returns></returns>
227+
public Dictionary<string, object[]> CompareState(TEntity newdata)
228+
{
229+
if (newdata == null) return null;
230+
if (_table.Primarys.Any() == false) throw new Exception($"不可比较,实体没有主键:{_db.OrmOriginal.GetEntityString(_entityType, newdata)}");
231+
var key = _db.OrmOriginal.GetEntityKeyString(_entityType, newdata, false);
232+
if (string.IsNullOrEmpty(key)) throw new Exception($"不可比较,未设置主键的值:{_db.OrmOriginal.GetEntityString(_entityType, newdata)}");
233+
if (_states.TryGetValue(key, out var oldState) == false || oldState == null)
234+
return _table.ColumnsByCs.ToDictionary(a => a.Key, a => new object[]
235+
{
236+
_db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, newdata, a.Key),
237+
null
238+
});
239+
240+
return _db.OrmOriginal.CompareEntityValueReturnColumns(_entityType, oldState.Value, newdata, false).ToDictionary(a => a, a => new object[]
241+
{
242+
_db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, newdata, a),
243+
_db.OrmOriginal.GetEntityValueWithPropertyName(_entityType, oldState.Value, a)
244+
});
245+
}
246+
222247
/// <summary>
223248
/// 清空状态数据
224249
/// </summary>

FreeSql.DbContext/FreeSql.DbContext.xml

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreeSql.DbContext/Repository/Repository/BaseRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public IBaseRepository<TEntity> AttachOnlyPrimary(TEntity data)
127127
return this;
128128
}
129129
public void FlushState() => _dbset.FlushState();
130+
public Dictionary<string, object[]> CompareState(TEntity newdata) => _dbset.CompareState(newdata);
130131

131132
public virtual TEntity InsertOrUpdate(TEntity entity)
132133
{

FreeSql.DbContext/Repository/Repository/IBaseRepository.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ public interface IBaseRepository<TEntity> : IBaseRepository
5858
/// </summary>
5959
/// <param name="data"></param>
6060
IBaseRepository<TEntity> AttachOnlyPrimary(TEntity data);
61+
/// <summary>
62+
/// 比较实体,计算出值发生变化的属性,以及属性变化的前后值
63+
/// </summary>
64+
/// <param name="newdata">最新的实体对象,它将与附加实体的状态对比</param>
65+
/// <returns></returns>
66+
Dictionary<string, object[]> CompareState(TEntity newdata);
6167

6268
int Update(TEntity entity);
6369
int Update(IEnumerable<TEntity> entitys);

FreeSql.Tests/FreeSql.Tests.DbContext/RepositoryTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public async Task Updatemysql()
2828
items[0].Title = "88";
2929
//items[1].Title = "88";
3030
items[2].Title = "88";
31+
var changed = repos.CompareState(items[0]);
3132
int x = await repos.UpdateAsync(items);
3233
}
3334

0 commit comments

Comments
 (0)