To figure out which property (or field) holds the entity's identifier, the convention
FindIdentityProperty is called.
By default, it looks for the property or the field named
Id (case sensitive). However, this property can
null value or even not be present at all. Then the automatic identifier generation strategy is performed.
The default convention is that entities get the identifiers in the following format
RavenDB client first determines the name of the collection that
the entity belongs to, then contacts the server to retrieve a numeric range of values. These values
can be used as the
The range of available numbers is calculated by using the
HiLo algorithm and is tracked per collection.
The current maximum value in ranges is stored in documents
Let's see the example.
var order = new Order
Id = null // value not provided
What will be the identifier of this order? You can check it by calling:
var orderId = session.Advanced.GetDocumentId(order); // "orders/1-A"
If this is the first
Order entity in your database, then it will return
orders/1-A. How does the identifier
generation process proceed? The RavenDB client determines the collection name as
(by default it is the plural form of the entity name).
Then it asks the server to give him the ID's range he can use (the first available range is 1 - 32). The server will
handle the Raven/Hilo/orders document.
The next available identifier value (always incrementing number) from the given range is
1 so its combination with
the collection name and the node tag gives the result
The next attempt to store another
Order object within the same session will result in creating the
identifier. However, this time asking the server about the possible range will not be necessary because the in-memory range
(1 - 32) is enough, so simply the next number will be added as the identifier suffix.
Identifier value numeric range generation
Each (in code) document store instance handles the generation of the identifier value numeric range. The database
stores the last requested number while the document store instances request ranges and caches the returned range of
The database has a single document (per collection) which stores the last identifier value requested by a document
E.g. the document
Raven/HiLo/accounts has the following value
then the next range will be
4001 - 4032, if 32 was range size (by default, it's 32).
The number of sessions per document store instance plays no part in identifiers value generation. When the store is
disposed of, the client sends the server the last value it used and the max value it got from the server.
Then the server will write it in the HiLo document (If the Max number is equal to the max number from the client
and bigger or equal to the last used value by the client)
If you intend to skip the identifier creation strategy that relies on the collection and HiLo value pair,
you can allow RavenDB to assign the Guid identifier to the stored document. Then, you have to provide the
string.Empty as the value of the
var orderEmptyId = new Order
Id = string.Empty // database will create a GUID value for it
var guidId = session.Advanced.GetDocumentId(orderEmptyId); // "bc151542-8fa7-45ac-bc04-509b343a8720"
This time the check for the document ID is called after
SaveChanges because only then we go to the server while
the entity's identifier is generated there.