/// <summary> /// Add an object into the ChangeTracker. /// </summary> /// <typeparam name="T">Must be a ITrackableEntity</typeparam> /// <param name="entry">The object instance.</param> /// <param name="saveToTemp">Should save in temporary edited objects or already applied changes.</param> public void Add <T>(EntityEntry <T> entry, bool saveToTemp = false) where T : class, ITrackableEntity { var objectKey = new StoreKey(entry); var key = GetKey(objectKey); var storedTempKeys = ReadTempStoredKeys().ToList(); var storedKeys = ReadStoredKeys().ToList(); var changedProperties = entry.GetChangedProperties(); if ((changedProperties?.Count() ?? 0) > 0) { if (saveToTemp) { if (!storedTempKeys.Contains(objectKey)) { storedTempKeys.Add(objectKey); SetTempStoredKeys(storedTempKeys.ToArray()); } cache.Set("TEMP_" + key, changedProperties.SerializeStoredObject()); } else { if (storedTempKeys.Contains(objectKey)) { storedTempKeys.Remove(objectKey); SetTempStoredKeys(storedTempKeys.ToArray()); cache.Remove("TEMP_" + key); } if (!storedKeys.Contains(objectKey)) { storedKeys.Add(objectKey); SetStoredKeys(storedKeys.ToArray()); } cache.Set(key, changedProperties.SerializeStoredObject()); } } else { if (storedTempKeys.Contains(objectKey)) { cache.Remove("TEMP_" + key); } if (storedKeys.Contains(objectKey)) { cache.Remove(key); } } }