Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How-To get consecutive id with RavenDb default id generator

Tags:

c#

.net

ravendb

I'm evaluating RavenDB for a new project.

If i create 100 entities i got great consecutive ids like :

  • posts/1
  • posts/2
  • posts/3
  • ...
  • posts/100

But if i build a new DocumentStore instance (after App Restart) and try to create new entities i got strange ids like this :

  • posts/1025
  • posts/1026
  • posts/1027

Any help ?

Note : I'm using Embedded Server with ASP.NET MVC 3

like image 733
Yoann. B Avatar asked May 18 '11 08:05

Yoann. B


2 Answers

This is by design - new HiLo keys are generated whenever you create a DocumentStore instance, so the gaps you are seeing are the unused ids from the other session.

Why do you care for consecutive ids?

This may be a good read on the subject, too: http://groups.google.com/group/ravendb/browse_thread/thread/3dbcacbc8b366ff8/

like image 124
synhershko Avatar answered Oct 01 '22 19:10

synhershko


From the RavenDb documents, you're after the Identity strategy.

RavenDB also supports the notion of Identity, for example if you need IDs to be consecutive. By creating a string Id property in your entity, and setting it to a value ending with a slash (/), you can tell RavenDB to use that as a key perfix for your entity. That prefix followed by the next available integer ID for it will be your entity's ID after you call SaveChanges().

eg.

var foo = new Foo();
foo.Id = "foo/"; // <-- this will use the Identity strategy, not HiLo.

session.Store(foo);
session.SaveChanges();
like image 33
Pure.Krome Avatar answered Oct 01 '22 20:10

Pure.Krome