0

Following the samples in the RavenDB Attachment docs and RavenDB Attachments - Functionality how to do?, I'm trying to add attachments to a Raven instance with the following code:

foreach (var currentDoc in docsToStore) {
  byte[] buff = ReadBytesFromFile(currentDoc.FilePath);
  var attachmentId = "attachedpages/" + attachmentCounter;
  var stream = new MemoryStream(buff);

  documentStore.DatabaseCommands.PutAttachment(attachmentId, null, stream, null);
  currentDoc.Attachments.Add(attachmentId);
  session.Store(currentDoc);  //Add the new document to Raven
}
session.saveChanges();

I've looked in the debugger to confirm that the MemoryStream has the data I expect. I also see the reference to currentDoc in the management studio. However, http://localhost:8080/static/?start=0&pagesize=128 simply returns an empty array.

Is there another step I need to take in order to save attachments?

Community
  • 1
  • 1
Brian
  • 3,850
  • 3
  • 21
  • 37

2 Answers2

2

The problem is that documentStore.DatabaseCommands will return a db commands for the default database.

You probably need to do:

documentStore.DatabaseCommands.ForDatabase("Mydb").PutAttachment
Ayende Rahien
  • 22,925
  • 1
  • 36
  • 41
0

The problem was that http://localhost:8080/static/?start=0&pagesize=128 wasn't showing my attachments. When I looked for them with either:

documentStore.DatabaseCommands.GetAttachmentHeadersStartingWith("", 0, 128);

documentStore.DatabaseCommands.ForDatabase("Mydb")
    .GetAttachmentHeadersStartingWith("", 0, 128);

Then I saw all the attachments I'd been trying to save.

Brian
  • 3,850
  • 3
  • 21
  • 37