Пример #1
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);
        }
Пример #2
0
        IEnumerable <TEntity> Enumerate()
        {
            foreach (var entity in AddedEntities.ToArray())
            {
                yield return(entity);
            }

            var ids =
                Changes.Values.Select(ch => ch.Id)
                .Concat(Table.Relation.Keys)
                .ToArray()
                .Distinct();

            foreach (var id in ids)
            {
                var entity = Find(id);
                if (entity != null)
                {
                    yield return(entity);
                }
            }
        }