public virtual InternalEntityEntry PrepareToSave() { if (EntityState == EntityState.Added) { var setProperty = EntityType.GetProperties().FirstOrDefault(p => p.IsReadOnlyBeforeSave && !IsTemporaryOrDefault(p)); if (setProperty != null) { throw new InvalidOperationException(CoreStrings.PropertyReadOnlyBeforeSave(setProperty.Name, EntityType.DisplayName())); } } else if (EntityState == EntityState.Modified) { var modifiedProperty = EntityType.GetProperties().FirstOrDefault(p => p.IsReadOnlyAfterSave && IsModified(p)); if (modifiedProperty != null) { throw new InvalidOperationException(CoreStrings.PropertyReadOnlyAfterSave(modifiedProperty.Name, EntityType.DisplayName())); } } if (EntityType.StoreGeneratedCount() > 0) { _storeGeneratedValues = new StoreGeneratedValues(this); } return(this); }
public virtual void AcceptChanges() { if (!_storeGeneratedValues.IsEmpty) { var storeGeneratedValues = _storeGeneratedValues; _storeGeneratedValues = new StoreGeneratedValues(); foreach (var property in EntityType.GetProperties()) { object value; if (storeGeneratedValues.TryGetValue(property, out value)) { this[property] = value; } } } var currentState = EntityState; if ((currentState == EntityState.Unchanged) || (currentState == EntityState.Detached)) { return; } if ((currentState == EntityState.Added) || (currentState == EntityState.Modified)) { _originalValues.AcceptChanges(this); SetEntityState(EntityState.Unchanged, true); } else if (currentState == EntityState.Deleted) { SetEntityState(EntityState.Detached); } }
public virtual void DiscardStoreGeneratedValues() => _storeGeneratedValues = new StoreGeneratedValues();