public virtual void UpdateDependentMap(InternalEntityEntry entry, EntityKey oldKey, IForeignKey foreignKey) { if (entry.EntityState == EntityState.Detached) { return; } var newKey = entry.GetDependentKeyValue(foreignKey); if (oldKey.Equals(newKey)) { return; } Dictionary<EntityKey, HashSet<InternalEntityEntry>> fkMap; if (_dependentsMap.TryGetValue(foreignKey, out fkMap)) { HashSet<InternalEntityEntry> dependents; if (oldKey != EntityKey.InvalidEntityKey && fkMap.TryGetValue(oldKey, out dependents)) { dependents.Remove(entry); if (dependents.Count == 0) { fkMap.Remove(oldKey); } } if (newKey == EntityKey.InvalidEntityKey) { if (fkMap.Count == 0) { _dependentsMap.Remove(foreignKey); } } else { if (!fkMap.TryGetValue(newKey, out dependents)) { dependents = new HashSet<InternalEntityEntry>(); fkMap[newKey] = dependents; } dependents.Add(entry); } } }
public virtual void UpdateIdentityMap(InternalEntityEntry entry, EntityKey oldKey, IKey principalKey) { if (entry.EntityState == EntityState.Detached) { return; } var newKey = GetKeyValueChecked(principalKey, entry); if (oldKey.Equals(newKey)) { return; } InternalEntityEntry existingEntry; if (_identityMap.TryGetValue(newKey, out existingEntry) && existingEntry != entry) { throw new InvalidOperationException(CoreStrings.IdentityConflict(entry.EntityType.Name)); } _identityMap.Remove(oldKey); if (newKey != EntityKey.InvalidEntityKey) { _identityMap[newKey] = entry; } }