efcore 重复跟踪问题解决,以及数组json列,无数据的时候会更新奇怪问题发现
continuous-integration/drone/push Build is passing Details

Test_IRC_Net8
hang 2026-01-19 23:46:44 +08:00 committed by hang
parent e6458e66df
commit 61ab33b3f3
1 changed files with 5 additions and 2 deletions

View File

@ -119,7 +119,10 @@ namespace IRaCIS.Core.Infra.EFCore
/// <summary>EntityState.Detached的实体 修改 部分字段</summary>
public static void EntityModifyPartialFiled<T>(this IRaCISDBContext _dbContext, T waitModifyEntity, Expression<Func<T, T>> updateFactory) where T : Entity
{
var entityEntry = _dbContext.Entry(waitModifyEntity);
//解决重复跟踪问题
var tracked = _dbContext.ChangeTracker.Entries<T>().FirstOrDefault(e => e.Entity.Id.Equals(waitModifyEntity.Id));
var entityEntry = tracked?? _dbContext.Entry(waitModifyEntity);
//entityEntry.State = EntityState.Detached;
var list = ((MemberInitExpression)updateFactory.Body).Bindings.Select(mb => mb.Member.Name)
@ -134,7 +137,7 @@ namespace IRaCIS.Core.Infra.EFCore
foreach (PropertyInfo prop in list)
{
_dbContext.Entry(waitModifyEntity).Property(prop.Name).IsModified = true;
entityEntry.Property(prop.Name).IsModified = true;
object value = prop.GetValue(applyObj);
prop.SetValue(waitModifyEntity, value);