Exemple #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);
        }
Exemple #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);
        }
Exemple #3
0
        public override int SaveChanges()
        {
            var count = 0;

            var relation      = Table.Relation;
            var addedEntities = AddedEntities.ToArray();
            var changes       = Changes.Values.ToArray();

            AddedEntities.Clear();
            Changes.Clear();
            Local.Clear();

            foreach (var entity in addedEntities)
            {
                AddToRelation(entity);
                count++;
            }

            foreach (var change in changes)
            {
                switch (change.ChangeType)
                {
                case ChangeType.Add:
                    AddToRelation(change.Entity);
                    count++;
                    break;

                case ChangeType.Remove:
                    if (!relation.Remove(change.Id))
                    {
                        ThrowRemovingNonexistingKeyException();
                    }
                    count++;
                    break;

                case ChangeType.Update:
                    var values = EntityOperation.NonkeyValues(change.Entity);
                    if (!values.SequenceEqual(change.Original))
                    {
                        // Throws if the key not found.
                        relation[change.Id] = change.Entity;
                        count++;
                    }
                    break;

                default:
                    throw new Exception("changeType = " + change.ChangeType);
                }
            }

            return(count);
        }