private IEntityDifference <T, TRelation> GetDifference <T, TRelation>(ICollectionRelationConfig <T, TRelation> relation, IDictionary <object, TRelation> persistedMapped, IList <TRelation> persistedUnmapped, IDictionary <object, TRelation> updatedMapped, IList <TRelation> updatedUnmapped) { var difference = new EntityDifference <T, TRelation>(); foreach (var key in updatedMapped.Keys) { var persisted = default(TRelation); persistedMapped.TryGetValue(key, out persisted); var updated = default(TRelation); updatedMapped.TryGetValue(key, out updated); difference.Updated.Add(new EntityDifference <TRelation>(persisted, updated)); } { foreach (var updated in updatedUnmapped) { difference.Added.Add(new EntityDifference <TRelation>(default(TRelation), updated)); } } foreach (var key in persistedMapped.Keys) { var persisted = default(TRelation); persistedMapped.TryGetValue(key, out persisted); if (updatedMapped.ContainsKey(key)) { continue; } difference.Deleted.Add(new EntityDifference <TRelation>(persisted, default(TRelation))); } return(difference); }
/// <summary> /// 修改数据 /// </summary> public static void Update(this DbContext context, IChangeManager changeManager, object entry) { if (entry == null) { throw new RepositoryDataException("Update newestEntity and originalEntity Can not be empty"); } //删除 var removes = changeManager.GetDifferences(EntityChangeType.Remove); if (removes.IsNullOrAuy()) { foreach (var remove in removes) { context.Delete(remove.OldEntity); } } List <int> addEntity = new List <int>(); //遍历图形 设置对象修改或者添加 context.ChangeTracker.TrackGraph(entry, e => { int sourceHashCode = e.SourceEntry?.Entity.GetHashCode() ?? 0; int entityHashCode = e.Entry.Entity.GetHashCode(); EntityDifference change = changeManager.GetDifference(entityHashCode); //如果上级添加,子级也添加 if (addEntity.Contains(sourceHashCode)) { addEntity.Add(e.Entry.Entity.GetHashCode()); e.Entry.State = EntityState.Added; return; } if (change == null) { e.Entry.State = EntityState.Unchanged; return; } if (change.Type == EntityChangeType.Addition) { addEntity.Add(entityHashCode); e.Entry.State = EntityState.Added; return; } else { e.Entry.State = EntityState.Unchanged; foreach (var item in change.ChangePropertys) { e.Entry.Member(item.Name).IsModified = true; } } }); }
private bool RemoveDiff(EntityChange entityChange, Difference difference) { if (difference.Object2 != null) { return(false); } if (difference.Object1 == null) { return(false); } var diff = new EntityDifference(difference.Object1.GetType().FullName, EntityChangeType.Remove, difference.ParentPropertyName); diff.OldEntity = difference.Object1; entityChange.DeleteEntry.Add(diff); return(true); }
protected virtual void AddDifference <T, TRelation>(EntityDifference <T, TRelation> entries, TRelation persisted, TRelation updated) { var entry = new EntityDifference <TRelation>(persisted, updated); if (entry.Persisted != null && entry.Updated != null) { entries.Updated.Add(entry); } else if (entry.Persisted != null) { entries.Deleted.Add(entry); } else if (entry.Updated != null) { entries.Added.Add(entry); } }
private void ModifyDiff(EntityChange entityChange, Difference difference) { object parentObj = difference.ParentObject2; //生成哈希Key int key = parentObj.GetHashCode(); entityChange.ChangeDifference.TryGetValue(key, out EntityDifference diff); if (diff == null) { diff = new EntityDifference(parentObj.GetType().FullName, EntityChangeType.Modify, difference.ParentPropertyName); entityChange.ChangeDifference.Add(key, diff); } string name = difference.PropertyName.Substring(difference.PropertyName.LastIndexOf(".") + 1); EntityChangePropertys propertys = new EntityChangePropertys(name, difference.Object1, difference.Object2); diff.ChangePropertys.Add(propertys); }
/// <summary> /// 获取 /// </summary> /// <param name="difference"></param> /// <returns></returns> private bool AdditionDiff(EntityChange entityChange, Difference difference) { if (difference.Object2 == null) { return(false); } if (difference.Object1 != null) { return(false); } //新增数据 EntityDifference diff = new EntityDifference(difference.Object2.GetType().FullName, EntityChangeType.Addition, difference.ParentPropertyName); var key = difference.Object2.GetHashCode(); entityChange.ChangeDifference.Add(key, diff); return(true); }