Ejemplo n.º 1
0
        public virtual async Task <int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var entriesToSave = StateEntries
                                .Where(e => e.EntityState.IsDirty())
                                .Select(e => e.PrepareToSave())
                                .ToList();

            if (!entriesToSave.Any())
            {
                return(0);
            }

            try
            {
                var result = await SaveChangesAsync(entriesToSave, cancellationToken);

                // TODO: When transactions supported, make it possible to commit/accept at end of all transactions
                foreach (var entry in entriesToSave)
                {
                    entry.AutoCommitSidecars();
                    entry.AcceptChanges();
                }

                return(result);
            }
            catch
            {
                foreach (var entry in entriesToSave)
                {
                    entry.AutoRollbackSidecars();
                }
                throw;
            }
        }
Ejemplo n.º 2
0
        public virtual StateEntry GetPrincipal([NotNull] IPropertyBagEntry dependentEntry, [NotNull] IForeignKey foreignKey)
        {
            Check.NotNull(dependentEntry, "dependentEntry");
            Check.NotNull(foreignKey, "foreignKey");

            var dependentKeyValue = dependentEntry.GetDependentKeyValue(foreignKey);

            if (dependentKeyValue == EntityKey.NullEntityKey)
            {
                return(null);
            }

            // TODO: Perf: Add additional indexes so that this isn't a linear lookup
            var principals = StateEntries.Where(
                e => e.EntityType == foreignKey.ReferencedEntityType &&
                dependentKeyValue.Equals(e.GetPrincipalKeyValue(foreignKey))).ToArray();

            if (principals.Length > 1)
            {
                // TODO: Better exception message
                throw new InvalidOperationException("Multiple matching principals.");
            }

            return(principals.FirstOrDefault());
        }
Ejemplo n.º 3
0
        public virtual int SaveChanges()
        {
            var entriesToSave = StateEntries
                                .Where(e => e.EntityState.IsDirty())
                                .Select(e => e.PrepareToSave())
                                .ToList();

            if (!entriesToSave.Any())
            {
                return(0);
            }

            try
            {
                var result = SaveChanges(entriesToSave);

                // TODO: When transactions supported, make it possible to commit/accept at end of all transactions
                // Issue #744
                foreach (var entry in entriesToSave)
                {
                    entry.AutoCommitSidecars();
                    entry.AcceptChanges();
                }

                return(result);
            }
            catch
            {
                foreach (var entry in entriesToSave)
                {
                    entry.AutoRollbackSidecars();
                }
                throw;
            }
        }
Ejemplo n.º 4
0
        public virtual IEnumerable <StateEntry> GetDependents([NotNull] StateEntry principalEntry, [NotNull] IForeignKey foreignKey)
        {
            Check.NotNull(principalEntry, "principalEntry");
            Check.NotNull(foreignKey, "foreignKey");

            var principalKeyValue = principalEntry.GetPrincipalKeyValue(foreignKey);

            // TODO: Add additional indexes so that this isn't a linear lookup
            return(StateEntries.Where(
                       e => e.EntityType == foreignKey.EntityType &&
                       principalKeyValue.Equals(e.GetDependentKeyValue(foreignKey))));
        }