RavenDB adds integer to ID when ends with a slash
I use RavenDB 1.0.0.426
I just experienced a weird scenario when importing data from an external source into RavenDB:
I chose to use the same unique ID as the external source uses, prefixed with a certain string. But. When I store a document with an ID that ends with a '/', raven automatically adds a number to the end of the ID, causing the document to NOT overwrite existing document stored with the same id.
I have recreated a simple scenario to cause the error:
The type I save:
public class Foo
{
public string Id { get; set; }
public Foo(string id)
{
Id = id;
}
}
Method saving a doc with the same id 10 times and afterwards checks the document count:
public void RunTest(string id)
{
for (int i = 0; i < 10; i++)
{
using (var doc = new DocumentStore() { Url = "http://pc-009:8080/" })
{
doc.Initialize();
using (var session = doc.OpenSession())
{
session.Store(new Foo(id));
session.SaveChanges();
}
}
}
// Wait for the data to be persisted
Thread.Sleep(2000);
using (var doc = new DocumentStore() { Url = "http://pc-009:8080/" })
{
doc.Initialize();
using (var session = doc.OpenSession())
{
var foos = session.Query<Foo>();
int fooCount = foos.Count();
// ASSERT HERE THAT fooCount is 1
}
}
}
Running the method with "id1" successfully overwrites existing documents:
RunTest("id1"); // Works fine
Running method with "id1/" ends up creating 10 documents in the database:
RunTest("id1/"); // Results in multiple documents to be created
I know that if you do not define your own ID, raven will autogenerate doc-ids on the fly, but is the behavior I describe above by design?
开发者_StackOverflowThankyou in advance
Stephan, This is expected, when your key ends with /, it asks ravendb to use identity generation strategy. See the docs here: http://ravendb.net/documentation/docs-api-key-generation
If you want a key that ends with /, you can url encode the keys
精彩评论