public async Task RetrieveIdsLong <TEntity>(IContextAdapter context, TEntity[] entities)
        {
            var tableName = context.GetTableName <TEntity>();

            var entitiesLength = entities.Length;
            var minId          = Minus(await context.RunScalar <TValue>(RetrieveIdsSql(tableName, entitiesLength)), entitiesLength);
            var pkSetter       = ExpressHelper.GetPropSetter <TEntity, TValue>(context.GetPk <TEntity>().PropertyName).Compile();
            var id             = minId;

            foreach (var entity in entities)
            {
                id = AddOne(id);
                pkSetter(entity, id);
            }
        }
Пример #2
0
        public static async Task BulkInsert <T>(this IContextAdapter contextAdapter, IEnumerable <T> entities,
                                                bool keepIdentity = false)
        {
            var sqlTransaction = contextAdapter.GeTransaction();

            using var sqlBulkCopy = new SqlBulkCopy(sqlTransaction.Connection,
                                                    keepIdentity ? SqlBulkCopyOptions.KeepIdentity : SqlBulkCopyOptions.Default, sqlTransaction)
                  {
                      BulkCopyTimeout      = BulkCopyTimeout,
                      DestinationTableName = contextAdapter.GetTableName <T>()
                  };
            var table = contextAdapter.GetDataTable(entities);

            foreach (var column in table.Columns.OfType <DataColumn>().Select(o => o.ColumnName))
            {
                sqlBulkCopy.ColumnMappings.Add(column, column);
            }

            await sqlBulkCopy.WriteToServerAsync(table);
        }