private TrackedObject Track(MetaType mt, object obj, Dictionary <object, object> visited, bool recurse, int level) { var standardTrackedObject = (StandardTrackedObject)GetTrackedObject(obj); if (standardTrackedObject != null || visited.ContainsKey(obj)) { return(standardTrackedObject); } var isWeaklyTracked = level > 1; standardTrackedObject = new StandardTrackedObject(this, mt, obj, obj, isWeaklyTracked); if (standardTrackedObject.HasDeferredLoaders) { throw System.Data.Linq.Error.CannotAttachAddNonNewEntities(); } items.Add(obj, standardTrackedObject); Attach(obj); visited.Add(obj, obj); if (recurse) { foreach (var parent in services.GetParents(mt, obj)) { Track(parent.Type, parent.Item, visited, recurse, level + 1); } { foreach (var child in services.GetChildren(mt, obj)) { Track(child.Type, child.Item, visited, recurse, level + 1); } return(standardTrackedObject); } } return(standardTrackedObject); }
private void TrackUntrackedObjects(MetaType type, object item, Dictionary <object, object> visited) { if (!visited.ContainsKey(item)) { visited.Add(item, item); var trackedObject = tracker.GetTrackedObject(item); if (trackedObject == null) { trackedObject = tracker.Track(item); trackedObject.ConvertToNew(); } else if (trackedObject.IsDead || trackedObject.IsRemoved) { return; } foreach (var parent in services.GetParents(type, item)) { TrackUntrackedObjects(parent.Type, parent.Item, visited); } if (trackedObject.IsNew) { trackedObject.InitializeDeferredLoaders(); if (!trackedObject.IsPendingGeneration(trackedObject.Type.IdentityMembers)) { trackedObject.SynchDependentData(); var obj = services.InsertLookupCachedObject(trackedObject.Type, item); if (obj != item) { var trackedObject2 = tracker.GetTrackedObject(obj); if (trackedObject2.IsDeleted || trackedObject2.CanInferDelete()) { trackedObject.ConvertToPossiblyModified(trackedObject2.Original); trackedObject2.ConvertToDead(); services.RemoveCachedObjectLike(trackedObject.Type, item); services.InsertLookupCachedObject(trackedObject.Type, item); } else if (!trackedObject2.IsDead) { throw new DuplicateKeyException(item, System.Data.Linq.Strings.CantAddAlreadyExistingKey); } } } else { var cachedObjectLike = services.GetCachedObjectLike(trackedObject.Type, item); if (cachedObjectLike != null) { var trackedObject3 = tracker.GetTrackedObject(cachedObjectLike); if (trackedObject3.IsDeleted || trackedObject3.CanInferDelete()) { trackedObject.ConvertToPossiblyModified(trackedObject3.Original); trackedObject3.ConvertToDead(); services.RemoveCachedObjectLike(trackedObject.Type, item); services.InsertLookupCachedObject(trackedObject.Type, item); } } } } foreach (var child in services.GetChildren(type, item)) { TrackUntrackedObjects(child.Type, child.Item, visited); } } }