Exemplo n.º 1
0
        /// <inheritdoc />
        public Task <ITempTableQuery <T> > BulkInsertIntoTempTableAsync <T>(
            IEnumerable <T> entities,
            ITempTableBulkInsertOptions options,
            CancellationToken cancellationToken = default)
            where T : class
        {
            if (options is not SqlServerTempTableBulkInsertOptions sqlServerOptions)
            {
                sqlServerOptions = new SqlServerTempTableBulkInsertOptions(options);
            }

            return(BulkInsertIntoTempTableAsync(entities, sqlServerOptions, cancellationToken));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public async Task <ITempTableQuery <T> > BulkInsertIntoTempTableAsync <T>(
            IEnumerable <T> entities,
            ITempTableBulkInsertOptions options,
            CancellationToken cancellationToken = default)
            where T : class
        {
            if (entities == null)
            {
                throw new ArgumentNullException(nameof(entities));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var entityType       = _ctx.Model.GetEntityType(typeof(T));
            var tempTableCreator = _ctx.GetService <ITempTableCreator>();

            if (!(options is SqliteTempTableBulkInsertOptions))
            {
                var sqliteOptions = new SqliteTempTableBulkInsertOptions(options);
                options = sqliteOptions;
            }

            var tempTableReference = await tempTableCreator.CreateTempTableAsync(entityType, options.TempTableCreationOptions, cancellationToken).ConfigureAwait(false);

            try
            {
                await BulkInsertAsync(entityType, entities, null, tempTableReference.Name, options.BulkInsertOptions, cancellationToken).ConfigureAwait(false);

                var query = _ctx.Set <T>().FromSqlRaw($"SELECT * FROM {_sqlGenerationHelper.DelimitIdentifier(tempTableReference.Name)}");

                return(new TempTableQuery <T>(query, tempTableReference));
            }
            catch (Exception)
            {
                await tempTableReference.DisposeAsync().ConfigureAwait(false);

                throw;
            }
        }