/// <summary>Executes deferred query lists.</summary>
        public void ExecuteQueries()
        {
            if (Queries.Count == 0)
            {
                // Already all executed
                return;
            }

#if EFCORE
            if (IsInMemory)
            {
                foreach (var query in Queries)
                {
                    query.ExecuteInMemory();
                }
                Queries.Clear();
                return;
            }
#endif

            if (Queries.Count == 1)
            {
                Queries[0].GetResultDirectly();
                Queries.Clear();
                return;
            }

            bool allowQueryBatch = QueryFutureManager.AllowQueryBatch;
#if EFCORE
            var databaseCreator = Context.Database.GetService <IDatabaseCreator>();

            var assembly     = databaseCreator.GetType().GetTypeInfo().Assembly;
            var assemblyName = assembly.GetName().Name;

            // We deactivated temporary some QueryFuture for EF Core as they don't work correctly
            // We need to still make them "work" for IncludeFilter feature
            var isMySqlPomelo = assemblyName == "Pomelo.EntityFrameworkCore.MySql";
            var isOracle      = assemblyName == "Oracle.EntityFrameworkCore" || assemblyName == "Devart.Data.Oracle.Entity.EFCore";
            if (allowQueryBatch && (isOracle || isMySqlPomelo))
            {
                allowQueryBatch = false;
            }
#endif

            if (!allowQueryBatch)
            {
                foreach (var query in Queries)
                {
                    query.GetResultDirectly();
                }

                Queries.Clear();
                return;
            }

#if EF5 || EF6
            var connection = (EntityConnection)Context.Connection;

#if EF6
            if (Context.IsInMemoryEffortQueryContext())
            {
                foreach (var query in Queries)
                {
                    query.GetResultDirectly();
                }

                Queries.Clear();
                return;
            }
#endif
#elif EFCORE
            if (IsInMemory)
            {
                foreach (var query in Queries)
                {
                    query.ExecuteInMemory();
                }
                return;
            }

            var connection = Context.Database.GetDbConnection();

            var firstQuery = Queries[0];
#endif
            var command = CreateCommandCombined();

            var ownConnection = false;

            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                    ownConnection = true;
                }

                using (command)
                {
#if EF5
                    using (var reader = command.ExecuteReader())
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
#elif EF6
                    var interceptionContext = Context.GetInterceptionContext();
                    using (var reader = DbInterception.Dispatch.Command.Reader(command, new DbCommandInterceptionContext(interceptionContext)))
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
#elif EFCORE
                    using (var reader = command.ExecuteReader())
                    {
                        var createEntityDataReader = new CreateEntityDataReader(reader);
                        foreach (var query in Queries)
                        {
                            query.SetResult(createEntityDataReader);
                            reader.NextResult();
                        }
                    }
#endif
                }

                Queries.Clear();
            }
            finally
            {
                if (ownConnection)
                {
                    connection.Close();
                }
            }

#if EFCORE
            if (firstQuery.RestoreConnection != null)
            {
                firstQuery.RestoreConnection();
            }
#endif
        }
        /// <summary>Executes deferred query lists.</summary>
        public async Task ExecuteQueriesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (Queries.Count == 0)
            {
                // Already all executed
                return;
            }

#if EFCORE
            if (IsInMemory)
            {
                foreach (var query in Queries)
                {
                    query.ExecuteInMemory();
                }
                Queries.Clear();
                return;
            }
#endif

            if (Queries.Count == 1)
            {
                await Queries[0].GetResultDirectlyAsync(cancellationToken).ConfigureAwait(false);
                Queries.Clear();
                return;
            }

#if EF5 || EF6
            var connection = (EntityConnection)Context.Connection;
#elif EFCORE
            if (IsInMemory)
            {
                foreach (var query in Queries)
                {
                    query.ExecuteInMemory();
                }
                return;
            }

            var connection = Context.Database.GetDbConnection();

            var firstQuery = Queries[0];
#endif
            var command = CreateCommandCombined();

            var ownConnection = false;

            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    await connection.OpenAsync(cancellationToken).ConfigureAwait(false);

                    ownConnection = true;
                }

                using (command)
                {
#if EF5
                    using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            await reader.NextResultAsync(cancellationToken).ConfigureAwait(false);
                        }
                    }
#elif EF6
                    var interceptionContext = Context.GetInterceptionContext();
                    using (var reader = await DbInterception.Dispatch.Command.ReaderAsync(command, new DbCommandInterceptionContext(interceptionContext), cancellationToken).ConfigureAwait(false))
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            await reader.NextResultAsync(cancellationToken).ConfigureAwait(false);
                        }
                    }
#elif EFCORE
                    using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
                    {
                        var createEntityDataReader = new CreateEntityDataReader(reader);
                        foreach (var query in Queries)
                        {
                            query.SetResult(createEntityDataReader);
                            await reader.NextResultAsync(cancellationToken).ConfigureAwait(false);
                        }
                    }
#endif
                }

                Queries.Clear();
            }
            finally
            {
                if (ownConnection)
                {
                    connection.Close();
                }
            }

#if EFCORE
            if (firstQuery.RestoreConnection != null)
            {
                firstQuery.RestoreConnection();
            }
#endif
        }
        /// <summary>Executes deferred query lists.</summary>
        public void ExecuteQueries()
        {
#if EF5 || EF6
            var connection = (EntityConnection) Context.Connection;
#elif EF7
            var connection = Context.Database.GetDbConnection();
#endif
            var command = CreateCommandCombined();

            var ownConnection = false;

            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                    ownConnection = true;
                }

                using (command)
                {
#if EF5
                    using (var reader = command.ExecuteReader())
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
}
#elif EF6
                    var interceptionContext = Context.GetInterceptionContext();
                    using (var reader = DbInterception.Dispatch.Command.Reader(command, new DbCommandInterceptionContext(interceptionContext)))
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
#elif EF7
                    using (var reader = command.ExecuteReader())
                    {
                        var createEntityDataReader = new CreateEntityDataReader(reader);
                        foreach (var query in Queries)
                        {
                            query.SetResult(createEntityDataReader);
                            reader.NextResult();
                        }
                    }
#endif
                }

                Queries.Clear();
            }
            finally
            {
                if (ownConnection)
                {
                    connection.Close();
                }
            }
        }
        /// <summary>Executes deferred query lists.</summary>
        public void ExecuteQueries()
        {
#if EF5 || EF6
            var connection = (EntityConnection)Context.Connection;
#elif EFCORE
            var connection = Context.Database.GetDbConnection();
#endif
            var command = CreateCommandCombined();

            var ownConnection = false;

            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                    ownConnection = true;
                }

                using (command)
                {
#if EF5
                    using (var reader = command.ExecuteReader())
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
#elif EF6
                    var interceptionContext = Context.GetInterceptionContext();
                    using (var reader = DbInterception.Dispatch.Command.Reader(command, new DbCommandInterceptionContext(interceptionContext)))
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
#elif EFCORE
                    using (var reader = command.ExecuteReader())
                    {
                        var createEntityDataReader = new CreateEntityDataReader(reader);
                        foreach (var query in Queries)
                        {
                            query.SetResult(createEntityDataReader);
                            reader.NextResult();
                        }
                    }
#endif
                }

                Queries.Clear();
            }
            finally
            {
                if (ownConnection)
                {
                    connection.Close();
                }
            }
        }
Пример #5
0
        /// <summary>Executes deferred query lists.</summary>
        public void ExecuteQueries()
        {
            if (Queries.Count == 0)
            {
                // Already all executed
                return;
            }

#if EFCORE
            if (IsInMemory)
            {
                foreach (var query in Queries)
                {
                    query.ExecuteInMemory();
                }
                Queries.Clear();
                return;
            }
#endif

            if (Queries.Count == 1)
            {
                Queries[0].GetResultDirectly();
                Queries.Clear();
                return;
            }

            if (!QueryFutureManager.AllowQueryBatch)
            {
                foreach (var query in Queries)
                {
                    query.GetResultDirectly();
                }

                Queries.Clear();
                return;
            }

#if EF5 || EF6
            var connection = (EntityConnection)Context.Connection;

#if EF6
            if (Context.IsInMemoryEffortQueryContext())
            {
                foreach (var query in Queries)
                {
                    query.GetResultDirectly();
                }

                Queries.Clear();
                return;
            }
#endif
#elif EFCORE
            if (IsInMemory)
            {
                foreach (var query in Queries)
                {
                    query.ExecuteInMemory();
                }
                return;
            }

            var connection = Context.Database.GetDbConnection();

            var firstQuery = Queries[0];
#endif
            var command = CreateCommandCombined();

            var ownConnection = false;

            try
            {
                if (connection.State != ConnectionState.Open)
                {
                    connection.Open();
                    ownConnection = true;
                }

                using (command)
                {
#if EF5
                    using (var reader = command.ExecuteReader())
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
#elif EF6
                    var interceptionContext = Context.GetInterceptionContext();
                    using (var reader = DbInterception.Dispatch.Command.Reader(command, new DbCommandInterceptionContext(interceptionContext)))
                    {
                        foreach (var query in Queries)
                        {
                            query.SetResult(reader);
                            reader.NextResult();
                        }
                    }
#elif EFCORE
                    using (var reader = command.ExecuteReader())
                    {
                        var createEntityDataReader = new CreateEntityDataReader(reader);
                        foreach (var query in Queries)
                        {
                            query.SetResult(createEntityDataReader);
                            reader.NextResult();
                        }
                    }
#endif
                }

                Queries.Clear();
            }
            finally
            {
                if (ownConnection)
                {
                    connection.Close();
                }
            }

#if EFCORE
            if (firstQuery.RestoreConnection != null)
            {
                firstQuery.RestoreConnection();
            }
#endif
        }