---
title: "Bulk Insert Attachments"
url: "https://docs.ravendb.net/6.2/document-extensions/attachments/bulk-insert"
version: "6.2"
language: "csharp"
---

# Bulk Insert Attachments

> Client language: C#. Also available for [Python](https://docs.ravendb.net/6.2/document-extensions/attachments/bulk-insert.python.md), [Node.js](https://docs.ravendb.net/6.2/document-extensions/attachments/bulk-insert.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - [BulkInsert](https://docs.ravendb.net/6.2/client-api/bulk-insert/how-to-work-with-bulk-insert-operation.md) is RavenDB's high-performance data insertion operation.  
>   Use its `AttachmentsFor` interface to add attachments to documents with great speed.
>
> - In this page:
>   - [Usage flow](#usage-flow)
>   - [Usage example](#usage-example)
>   - [Syntax](#syntax)

## Usage flow

- Create a `BulkInsert` instance.

- Pass the Document ID to the instance's `AttachmentsFor` method.

- To add an attachment, call `Store`.  
  Pass it the attachment's name, stream, and type (optional).  
  The `Store` function can be called repeatedly as necessary.

- Note:  
  If an attachment with the specified name already exists on the document,  
  the bulk insert operation will overwrite it.

## Usage example

In this example, we attach a file to all User documents that match a query.

### BulkInsert\_Attachments

```csharp
List<User> users;

// Choose user profiles for which to attach a file
using (var session = store.OpenSession())
{
  users = session.Query<User>()
      .Where(u => u.Age < 30)
      .ToList();
}

// Prepare content to attach
byte[] byteArray = Encoding.UTF8.GetBytes("some contents here");
var stream = new MemoryStream(byteArray);

// Create a BulkInsert instance
using (var bulkInsert = store.BulkInsert())
{
  for (var i = 0; i < users.Count; i++)
  {
      string userId = users[i].Id;
      
      // Call 'AttachmentsFor', pass the document ID for which to attach the file
      var attachmentsBulkInsert = bulkInsert.AttachmentsFor(userId);

      // Call 'Store' to add the file to the BulkInsert instance
      // The data stored in bulkInsert will be streamed to the server in batches 
      attachmentsBulkInsert.Store("AttachmentName", stream);
  }
}
```

### User\_Class

```csharp
public class User
{
  public string Id { get; set; }
  public string Name { get; set; }
  public string LastName { get; set; }
  public string AddressId { get; set; }
  public int Count { get; set; }
  public int Age { get; set; }
}
```

## Syntax

```csharp
public AttachmentsBulkInsert AttachmentsFor(string id)
```

| Parameter | Type | Description |
| - | - | - |
| `id` | `string` | The document ID to which the attachment should be added. |

```csharp
public void Store(string name, Stream stream, string contentType = null)
```

| Parameter | Type | Description |
| - | - | - |
| `name` | `string` | Name of attachment |
| `stream` | `Stream` | The attachment's stream |
| `contentType` | `string` | Type of attachment (default: null) |

## See also

- [Storing](https://docs.ravendb.net/6.2/document-extensions/attachments/storing.md)
- [Loading](https://docs.ravendb.net/6.2/document-extensions/attachments/loading.md)
- [Deleting](https://docs.ravendb.net/6.2/document-extensions/attachments/deleting.md)
