public static async Task <EntityEntry> RefreshAsync(this EntityEntry tracking, RefreshConflict refreshMode) { switch (refreshMode) { case RefreshConflict.StoreWins: { await tracking.ReloadAsync(); break; } case RefreshConflict.ClientWins: { PropertyValues databaseValues = await tracking.GetDatabaseValuesAsync(); if (databaseValues == null) { tracking.State = EntityState.Detached; } else { tracking.OriginalValues.SetValues(databaseValues); } break; } case RefreshConflict.MergeClientAndStore: { PropertyValues databaseValues = await tracking.GetDatabaseValuesAsync(); if (databaseValues == null) { tracking.State = EntityState.Detached; } else { PropertyValues originalValues = tracking.OriginalValues.Clone(); #if !EF originalValues.SetValues(tracking.OriginalValues); #endif tracking.OriginalValues.SetValues(databaseValues); #if EF databaseValues.PropertyNames .Where(property => !object.Equals(originalValues[property], databaseValues[property])) .ForEach(property => tracking.Property(property).IsModified = false); #else databaseValues.Properties .Where(property => !object.Equals(originalValues[property.Name], databaseValues[property.Name])) .ForEach(property => tracking.Property(property.Name).IsModified = false); #endif } break; } } return(tracking); }
/// <summary> /// Gets or sets the value of the property with the specified property name. /// The value may be a nested instance of this class. /// </summary> /// <param name="propertyName"> The property name. </param> /// <value> The value of the property. </value> public object this[string propertyName] { get { Check.NotEmpty(propertyName, "propertyName"); var value = _internalValues[propertyName]; var asValues = value as InternalPropertyValues; if (asValues != null) { value = new DbPropertyValues(asValues); } return(value); } set { Check.NotEmpty(propertyName, "propertyName"); _internalValues[propertyName] = value; } }
/// <summary> /// Sets the values of this dictionary by reading values from another dictionary. /// The other dictionary must be based on the same type as this dictionary, or a type derived /// from the type for this dictionary. /// </summary> /// <param name="propertyValues"> The dictionary to read values from. </param> public void SetValues(DbPropertyValues propertyValues) { Check.NotNull(propertyValues, "propertyValues"); _internalValues.SetValues(propertyValues._internalValues); }
/// <summary> /// Sets the values of this dictionary by reading values from another dictionary. /// The other dictionary must be based on the same type as this dictionary, or a type derived /// from the type for this dictionary. /// </summary> /// <param name="propertyValues"> The dictionary to read values from. </param> public void SetValues(DbPropertyValues propertyValues) { Check.NotNull <DbPropertyValues>(propertyValues, nameof(propertyValues)); this._internalValues.SetValues(propertyValues._internalValues); }
internal void WriteAudit(SADFMEntities context, Guid lastUpdateAccountId) { if (!context.ChangeTracker.HasChanges()) { return; } var allChanges = context.ChangeTracker.Entries(); foreach (DbEntityEntry ee in allChanges) { if (ee.State == System.Data.Entity.EntityState.Unchanged) { continue; } if (ee.State == System.Data.Entity.EntityState.Deleted) { //Deletes should be tracked by the parent object or status change continue; } ObjectContext ox = ((IObjectContextAdapter)context).ObjectContext; ObjectStateEntry ose = ox.ObjectStateManager.GetObjectStateEntry(ee.Entity); Guid pkid = Guid.Empty; if (ose.EntityKey.EntityKeyValues != null) { EntityKeyMember member = ose.EntityKey.EntityKeyValues[0]; pkid = (Guid)member.Value; } bool added = ee.State == System.Data.Entity.EntityState.Added; string tableName = ose.EntitySet.Name; System.Data.Entity.Infrastructure.DbPropertyValues original = null; if (!added) { original = ee.OriginalValues; } System.Data.Entity.Infrastructure.DbPropertyValues current = null; if (ee.State != System.Data.Entity.EntityState.Deleted) { current = ee.CurrentValues; } foreach (string property in current.PropertyNames) { object originalValue = original == null ? null : original.GetValue <object>(property); object currentValue = current == null ? null : current.GetValue <object>(property); if (originalValue == null && currentValue == null) { continue; } if (originalValue == null || currentValue == null || originalValue.ToString() != currentValue.ToString()) { context.Audits.Add(new Data.Audit { TableName = tableName, ChangeTypeId = DataAccess.ListItem.GetListItem("ChangeType", added ? "Insert" : "Update").GUID, AccountId = lastUpdateAccountId, FieldName = property, OldValue = originalValue == null ? null : originalValue.ToString().Substring(0, originalValue.ToString().Length > 1000 ? 1000 : originalValue.ToString().Length), NewValue = currentValue == null ? null : currentValue.ToString().Substring(0, currentValue.ToString().Length > 1000 ? 1000 : currentValue.ToString().Length), PrimaryKeyId = pkid, UpdateDate = DateTime.Now }); } } } }