public override void DetectChanges(InternalEntityEntry entry) { var entityType = entry.EntityType; var properties = entityType.GetProperties(); foreach (var property in properties) { var current = entry[property]; var old = entry.GetOriginalValue(property); var flag = current != null && current.GetType().Name == typeof(JsonObject<>).Name; TypeInfo type = null; PropertyInfo originalProperty = null; if (flag) { type = current.GetType().GetTypeInfo(); originalProperty = type.DeclaredProperties.Single(x => x.Name == "_originalValue"); old = (string)originalProperty.GetValue(current); var prop = type.DeclaredProperties.Single(x => x.Name == "Json"); current = (string)prop.GetValue(current); } if (property.GetOriginalValueIndex() >= 0 && (!Equals(entry[property], entry.GetOriginalValue(property)) || flag && current != old)) { entry.SetPropertyModified(property); if (flag) { originalProperty.SetValue(entry[property], current); } } } base.DetectChanges(entry); }
public static InternalEntityEntry GetPrincipaEntityEntryOriginalValue(this ChangeTracker changeTracker, InternalEntityEntry targetEntity, IForeignKey foreignKey) { IEnumerable <InternalEntityEntry> targetEntities = changeTracker.GetStateManager().Entries.Where(p => Equals(p.EntityType.ClrType, foreignKey.PrincipalEntityType.ClrType)); InternalEntityEntry principalEntityEntry = null; foreach (InternalEntityEntry entityEntry in targetEntities) { bool result = true; for (int i = 0; i < foreignKey.Properties.Count; i++) { object foreignValue = targetEntity.GetOriginalValue(foreignKey.Properties[i]); object principalValue = entityEntry.GetOriginalValue(foreignKey.PrincipalKey.Properties[i]); if (!Equals(foreignValue, principalValue)) { result = false; break; } } if (result) { principalEntityEntry = entityEntry; } } return(principalEntityEntry); }
public void StateChanging(InternalEntityEntry entry, EntityState newState) { Console.WriteLine( " Changing {0} from {1} to {2}.", entry.Entity.GetType().Name, entry.EntityState, newState); if (newState != EntityState.Added && newState != EntityState.Modified) { return; } var set = services.GetRequiredService <ICultureSet>(); var type = entry.Entity.GetType(); var properties = type.GetProperties().Where(y => y.GetCustomAttribute <LocalizedAttribute>() != null).ToList(); foreach (var y in properties) { try { var origin = entry.GetOriginalValue(entry.EntityType.FindProperty(y.Name)); var current = entry.GetCurrentValue(entry.EntityType.FindProperty(y.Name)); var cultureProvider = services.GetRequiredService <ICultureProvider>(); var culture = cultureProvider.DetermineCulture(); culture = set.SimplifyCulture(culture); Dictionary <string, string> json; if (newState == EntityState.Added) { json = new Dictionary <string, string>(); } else { json = JsonConvert.DeserializeObject <Dictionary <string, string> >(origin.ToString()); } if (json.ContainsKey(culture)) { json[culture] = current.ToString(); } else { json.Add(culture, current.ToString()); } entry.SetProperty(entry.EntityType.FindProperty(y.Name), JsonConvert.SerializeObject(json)); } catch { break; } } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void DetectChanges(InternalEntityEntry entry) { var entityType = entry.EntityType; foreach (var property in entityType.GetProperties()) { if (property.GetOriginalValueIndex() >= 0 && !entry.IsModified(property) && !entry.IsConceptualNull(property)) { var current = entry[property]; var original = entry.GetOriginalValue(property); var comparer = property.GetValueComparer() ?? property.FindMapping()?.Comparer; if (comparer == null) { if (!Equals(current, original)) { LogChangeDetected(entry, property, original, current); entry.SetPropertyModified(property); } } else { if (!comparer.Equals(current, original)) { LogChangeDetected(entry, property, original, current); entry.SetPropertyModified(property); } } } } foreach (var property in entityType.GetProperties()) { DetectKeyChange(entry, property); } if (entry.HasRelationshipSnapshot) { foreach (var navigation in entityType.GetNavigations()) { DetectNavigationChange(entry, navigation); } } }
public void StateChanging(InternalEntityEntry entry, EntityState newState) { if (!(newState == EntityState.Added && entry.EntityState == EntityState.Detached || newState == EntityState.Modified && entry.EntityState == EntityState.Unchanged)) { return; } var set = services.GetRequiredService <ICultureSet>(); var type = entry.Entity.GetType(); var properties = type.GetProperties().Where(y => y.GetCustomAttribute <LocalizedAttribute>() != null).ToList(); foreach (var y in properties) { try { var origin = entry.GetOriginalValue(entry.EntityType.FindProperty(y.Name)); var current = entry.GetCurrentValue(entry.EntityType.FindProperty(y.Name)); var cultureProvider = services.GetRequiredService <ICultureProvider>(); var culture = cultureProvider.DetermineCulture(); culture = set.SimplifyCulture(culture); Dictionary <string, string> dic; if (newState == EntityState.Added) { dic = new Dictionary <string, string>(); } else { dic = JsonConvert.DeserializeObject <Dictionary <string, string> >(origin.ToString()); } if (dic.ContainsKey(culture)) { dic[culture] = current.ToString(); } else { dic.Add(culture, current.ToString()); } var json = JsonConvert.SerializeObject(dic); writePropertyValue.Invoke(entry, new object[] { entry.EntityType.FindProperty(y.Name), json }); } catch (Exception ex) { break; } } }
/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public void DetectValueChange(InternalEntityEntry entry, IProperty property) { var current = entry[property]; var original = entry.GetOriginalValue(property); if (!property.GetValueComparer().Equals(current, original)) { if (entry.EntityState == EntityState.Deleted) { ThrowIfKeyChanged(entry, property); } else { LogChangeDetected(entry, property, original, current); entry.SetPropertyModified(property); } } }
public void StateChanging(InternalEntityEntry entry, EntityState newState) { Console.WriteLine( " Changing {0} from {1} to {2}.", entry.Entity.GetType().Name, entry.EntityState, newState); if (newState != EntityState.Added && newState != EntityState.Modified) return; var set = services.GetRequiredService<ICultureSet>(); var type = entry.Entity.GetType(); var properties = type.GetProperties().Where(y => y.GetCustomAttribute<LocalizedAttribute>() != null).ToList(); foreach (var y in properties) { try { var origin = entry.GetOriginalValue(entry.EntityType.FindProperty(y.Name)); var current = entry.GetCurrentValue(entry.EntityType.FindProperty(y.Name)); var cultureProvider = services.GetRequiredService<ICultureProvider>(); var culture = cultureProvider.DetermineCulture(); culture = set.SimplifyCulture(culture); Dictionary<string, string> json; if (newState == EntityState.Added) json = new Dictionary<string, string>(); else json = JsonConvert.DeserializeObject<Dictionary<string, string>>(origin.ToString()); if (json.ContainsKey(culture)) json[culture] = current.ToString(); else json.Add(culture, current.ToString()); entry.SetProperty(entry.EntityType.FindProperty(y.Name), JsonConvert.SerializeObject(json)); } catch { break; } } }
private static void DetectPropertyChanges(InternalEntityEntry entry) { var entityType = entry.EntityType; if (entityType.HasPropertyChangedNotifications()) { return; } foreach (var property in entityType.GetProperties()) { if (property.GetOriginalValueIndex() >= 0 && !Equals(entry[property], entry.GetOriginalValue(property))) { entry.SetPropertyModified(property); } } }
/// <summary> /// This API supports the Entity Framework Core infrastructure and is not intended to be used /// directly from your code. This API may change or be removed in future releases. /// </summary> public virtual void DetectChanges(InternalEntityEntry entry) { var entityType = entry.EntityType; foreach (var property in entityType.GetProperties()) { if (property.GetOriginalValueIndex() >= 0 && !Equals(entry[property], entry.GetOriginalValue(property))) { entry.SetPropertyModified(property); } } foreach (var property in entityType.GetProperties()) { DetectKeyChange(entry, property); } if (entry.HasRelationshipSnapshot) { foreach (var navigation in entityType.GetNavigations()) { DetectNavigationChange(entry, navigation); } } }