Пример #1
0
        public override TEntity Remove(TEntity entity)
        {
            var id = EntityOperation.GetSurrogateKey(entity);

            if (EntityOperation <TEntity> .IsValidSurrogateKey(id))
            {
                var change = default(Change);
                if (Changes.TryGetValue(id, out change))
                {
                    if (change.Entity != entity)
                    {
                        ThrowAttachingMoreThanOneEntityException();
                    }
                    change.ChangeType = ChangeType.Remove;
                }
                else
                {
                    var nonkeyValues = EntityOperation.NonkeyValues(entity);
                    change = new Change(entity, id, nonkeyValues, ChangeType.Remove);
                    Changes.Add(id, change);
                }
            }
            else
            {
                AddedEntities.Remove(entity);
            }
            return(entity);
        }
Пример #2
0
        public override TEntity Attach(TEntity entity)
        {
            var id = EntityOperation.GetSurrogateKey(entity);

            if (!EntityOperation <TEntity> .IsValidSurrogateKey(id))
            {
                throw new ArgumentException("Entity doesn't have valid ID.");
            }

            var change = default(Change);

            if (Changes.TryGetValue(id, out change))
            {
                if (change.Entity != entity)
                {
                    ThrowAttachingMoreThanOneEntityException();
                }
                change.ChangeType = ChangeType.Update;
            }
            else
            {
                var nonkeyValues = EntityOperation.NonkeyValues(entity);
                change = new Change(entity, id, nonkeyValues, ChangeType.Update);
                Changes.Add(id, change);
            }
            return(entity);
        }