BulkInsert error with RavenDB: Document id must have a non empty value

227 views Asked by At

I am using the BulkInsert operation of RavenDB 4.0.6 to insert a bunch of products:

    using (var bulk = Store.BulkInsert())
    {
        foreach (var p in products)
        {
            p.Id = string.Empty; // <-- notice this
            await bulk.StoreAsync(p);
        }
    }

Notice that I'm deliberately skipping the identifier creation strategy by explicitly providing string.Empty as the value of the Id property. This is based on the RavenDB docs, section Autogenerated ID's.

When running the code I get the error:

System.InvalidOperationException : Document id must have a non empty value

Which is directly produced by this codesnippet in BulkInsertOperation.cs.

My question is how I can prevent this error and still keep the same ID-generation strategy as my other code does?

E.g. I never set the Id property to anything other than string.Empty. And I'm afraid that setting it to, for example, Guid.NewGuid.ToString() might cause other issues (see this question as well).

1

There are 1 answers

3
Eric Damtoft On BEST ANSWER

For bulk operations, you have to either leave the Id property null (not empty string) to have it auto-generate a sequential ID, or generate a Guid ID manually.

The API is a bit inconsistent between bulk inserts and session inserts:

using (var store = new DocumentStore() { Urls = new[] { "http://localhost:8080" } }.Initialize())
{
  using (var bulk = store.BulkInsert("Sample"))
  {
    bulk.Store(new SampleDocument { Name = "Bulk.Store Null Id", Id = null }); // Sequential Id (I.E. SampleDocuments/1-A)
    bulk.Store(new SampleDocument { Name = "Bulk.Store Blank Id", Id = "" }); // Throws Error
    bulk.Store(new SampleDocument { Name = "Bulk.Store Guid Id", Id = Guid.NewGuid().ToString() }); // Guid Id
  }

  using (var session = store.OpenSession("Sample"))
  {
    session.Store(new SampleDocument { Name = "Session.Store Null Id", Id = null }); // Sequential Id (I.E. SampleDocuments/2-A)
    session.Store(new SampleDocument { Name = "Session.Store Empty Id", Id = "" }); // Guid Id
    session.Store(new SampleDocument { Name = "Session.Store Guid Id", Id = Guid.NewGuid().ToString() }); // Guid Id
    session.SaveChanges();
  }
}