How to get an identifier in a row using the default RavenDb identifier generator

I am evaluating RavenDB for a new project.

If I create 100 entities, I got great consecutive identifiers like:

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

But if I create a new instance of DocumentStore (after App Restart) and try to create new entities, I will get strange identifiers like:

  • Posts / 1025
  • Posts / 1026
  • Posts / 1027

Any help?

Note. I am using the embedded server with ASP.NET MVC 3

+6
source share
3 answers

This is by design - new HiLo keys are generated whenever you instantiate a DocumentStore, so the spaces you see are unused identifiers from another session.

Why do you need consecutive identifiers?

It may be useful to read on this subject: http://groups.google.com/group/ravendb/browse_thread/thread/3dbcacbc8b366ff8/

+7
source

From the RavenDb docs, you're following the Identity strategy .

RavenDB also supports the concept of Identity, for example, if you need Identifiers must be consistent. By creating the string Id property in your entity and assigning it to a value ending with a slash (/), you can tell RavenDB to use this as key perfectionism for your organization. This prefix is ​​followed by the next available integer identifier, it will be your face ID after calling SaveChanges ().

eg.

var foo = new Foo(); foo.Id = "foo/"; // <-- this will use the Identity strategy, not HiLo. session.Store(foo); session.SaveChanges(); 
+7
source

You might want to take a look at the authentication option for RavenDB, but this is not really what you should take care of.

+3
source

Source: https://habr.com/ru/post/888394/


All Articles