Example #1
0
        /// <summary>
        ///     Consumes the data reader created by <see cref="ReaderModificationCommandBatch.ExecuteAsync" />.
        /// </summary>
        /// <param name="reader"> The data reader. </param>
        /// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
        /// <returns> A task that represents the asynchronous operation. </returns>
        protected override async Task ConsumeAsync(
            RelationalDataReader reader,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Debug.Assert(CommandResultSet.Count == ModificationCommands.Count);
            var commandIndex = 0;

            try
            {
                var actualResultSetCount = 0;
                do
                {
                    while (commandIndex < CommandResultSet.Count &&
                           CommandResultSet[commandIndex] == ResultSetMapping.NoResultSet)
                    {
                        commandIndex++;
                    }

                    if (commandIndex < CommandResultSet.Count)
                    {
                        commandIndex = ModificationCommands[commandIndex].RequiresResultPropagation
                            ? await ConsumeResultSetWithPropagationAsync(commandIndex, reader, cancellationToken)
                            : await ConsumeResultSetWithoutPropagationAsync(commandIndex, reader, cancellationToken);

                        actualResultSetCount++;
                    }
                }while (commandIndex < CommandResultSet.Count &&
                        await reader.DbDataReader.NextResultAsync(cancellationToken));

#if DEBUG
                while (commandIndex < CommandResultSet.Count &&
                       CommandResultSet[commandIndex] == ResultSetMapping.NoResultSet)
                {
                    commandIndex++;
                }

                Debug.Assert(
                    commandIndex == ModificationCommands.Count,
                    "Expected " + ModificationCommands.Count + " results, got " + commandIndex);

                var expectedResultSetCount = CommandResultSet.Count(e => e == ResultSetMapping.LastInResultSet);

                Debug.Assert(
                    actualResultSetCount == expectedResultSetCount,
                    "Expected " + expectedResultSetCount + " result sets, got " + actualResultSetCount);
#endif
            }
            catch (DbUpdateException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DbUpdateException(
                          RelationalStrings.UpdateStoreException,
                          ex,
                          ModificationCommands[commandIndex].Entries);
            }
        }
        /// <summary>
        ///     Consumes the data reader created by <see cref="ReaderModificationCommandBatch.Execute" />.
        /// </summary>
        /// <param name="reader"> The data reader. </param>
        protected override void Consume(RelationalDataReader reader)
        {
            Check.DebugAssert(
                CommandResultSet.Count == ModificationCommands.Count,
                $"CommandResultSet.Count of {CommandResultSet.Count} != ModificationCommands.Count of {ModificationCommands.Count}");

            var commandIndex = 0;

            try
            {
                var actualResultSetCount = 0;
                do
                {
                    while (commandIndex < CommandResultSet.Count &&
                           CommandResultSet[commandIndex] == ResultSetMapping.NoResultSet)
                    {
                        commandIndex++;
                    }

                    if (commandIndex < CommandResultSet.Count)
                    {
                        commandIndex = ModificationCommands[commandIndex].RequiresResultPropagation
                            ? ConsumeResultSetWithPropagation(commandIndex, reader)
                            : ConsumeResultSetWithoutPropagation(commandIndex, reader);
                        actualResultSetCount++;
                    }
                }while (commandIndex < CommandResultSet.Count &&
                        reader.DbDataReader.NextResult());

#if DEBUG
                while (commandIndex < CommandResultSet.Count &&
                       CommandResultSet[commandIndex] == ResultSetMapping.NoResultSet)
                {
                    commandIndex++;
                }

                Check.DebugAssert(
                    commandIndex == ModificationCommands.Count,
                    "Expected " + ModificationCommands.Count + " results, got " + commandIndex);

                var expectedResultSetCount = CommandResultSet.Count(e => e == ResultSetMapping.LastInResultSet);

                Check.DebugAssert(
                    actualResultSetCount == expectedResultSetCount,
                    "Expected " + expectedResultSetCount + " result sets, got " + actualResultSetCount);
#endif
            }
            catch (Exception ex) when(ex is not DbUpdateException and not OperationCanceledException)
            {
                throw new DbUpdateException(
                          RelationalStrings.UpdateStoreException,
                          ex,
                          ModificationCommands[commandIndex].Entries);
            }
        }