Ejemplo n.º 1
0
        private void SetEntityState(EntityState newState, Tuple <IProperty, object>[] generatedValues)
        {
            // The entity state can be Modified even if some properties are not modified so always
            // set all properties to modified if the entity state is explicitly set to Modified.
            if (newState == EntityState.Modified)
            {
                _stateData.SetAllPropertiesModified(_entityType.Properties.Count());
            }

            var oldState = _stateData.EntityState;

            if (oldState == newState)
            {
                return;
            }

            // An Added entity does not yet exist in the database. If it is then marked as deleted there is
            // nothing to delete because it was not yet inserted, so just make sure it doesn't get inserted.
            if (oldState == EntityState.Added &&
                newState == EntityState.Deleted)
            {
                newState = EntityState.Unknown;
            }

            _configuration.Services.StateEntryNotifier.StateChanging(this, newState);

            _stateData.EntityState = newState;

            if (newState == EntityState.Added)
            {
                foreach (var generatedValue in generatedValues.Where(v => v != null))
                {
                    this[generatedValue.Item1] = generatedValue.Item2;
                }
            }
            else
            {
                Contract.Assert(generatedValues == null);
            }

            if (oldState == EntityState.Unknown)
            {
                _configuration.Services.StateManager.StartTracking(this);
            }
            else if (newState == EntityState.Unknown)
            {
                // TODO: Does changing to Unknown really mean stop tracking?
                _configuration.Services.StateManager.StopTracking(this);
            }

            _configuration.Services.StateEntryNotifier.StateChanged(this, oldState);
        }
Ejemplo n.º 2
0
        private void SetEntityState(EntityState entityState, object generatedValue)
        {
            // The entity state can be Modified even if some properties are not modified so always
            // set all properties to modified if the entity state is explicitly set to Modified.
            if (entityState == EntityState.Modified)
            {
                _stateData.SetAllPropertiesModified(_entityType.Properties.Count());
            }

            var oldState = _stateData.EntityState;

            if (oldState == entityState)
            {
                return;
            }

            _configuration.Services.StateEntryNotifier.StateChanging(this, entityState);

            _stateData.EntityState = entityState;

            if (entityState == EntityState.Added &&
                generatedValue != null)
            {
                this[_entityType.GetKey().Properties.Single()] = generatedValue; // TODO: Composite keys not implemented yet.
            }

            if (oldState == EntityState.Unknown)
            {
                _configuration.Services.StateManager.StartTracking(this);
            }
            else if (entityState == EntityState.Unknown)
            {
                // TODO: Does changing to Unknown really mean stop tracking?
                _configuration.Services.StateManager.StopTracking(this);
            }

            _configuration.Services.StateEntryNotifier.StateChanged(this, oldState);
        }