2

I just upgraded a project from RavenDB 3.5 to 4.0 and one of the biggest change I noticed is the way they change the way Ids are generated.

In my project most of the collections have a basic id structure like "[collection name]/[progressive id]", where the progressive id is an integer, and not the new default "[progressive]-[node]". Following the documentation I specified the pattern id for new documents as "[collection name]|" and is actually generating unique/progressive/integer ids.

The problem is when I've to save transactionally 2 or more documents and reference them between themselves. Let's say I've two kind of object:

User entity

{
    "Id": "users/1",
    ...
}

User address entity

{
    "Id": "userAddresses/1",
    "UserId": "users/1",
    ...
}

Where in the second document I need to reference the first one via the UserId field.

Before the version 4.0 I was able, in the same transaction, to do something like:

User newUser = new User();

session.Store(newUser)

UserAddress newUserAddress = new UserAddress();
newUserAddress.UserId = newUser.Id;

session.Store(newUserAddress);

session.SaveChanges();

After the session.Store(newUser) if I accessed the newUser.Id property I was able to see the generated Id. Now I just see "users|", I've to wait after the SaveChanges() to see the generated Ids.

This behaviour seems to happen only for Identities Ids, if I use the id structure "[collection name]/[progressive]-[node]" I'm able to see the generated id right after the Store().

Is it by design? Is there a way to force the old behaviour? OR How can I manage transactionally a situation like this one using progressive/integer ids?

tanathos
  • 5,566
  • 4
  • 34
  • 46

1 Answers1

3

In RavenDB v4.0 you have the same behavior. After you call to session.Store(entity), or await session.StoreAsync(entity) for the async session, you should have the entity.Id filled with the ID.

It is setting the ID using the HiLo approach, which you can read about it here: https://ravendb.net/docs/article-page/4.0/Csharp/server/kb/document-identifier-generation#hilo-algorithm

The only difference in RavenDB v4.0 that the ID would be like: users/1-A instead of users/1 in the previous versions.

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
  • This is the problem: I need it to work also for identities ids. I already tested the default pattern and is working as before. But as I use the "users" pipe format to have just integers, is not working anymore... – tanathos Mar 12 '18 at 11:56
  • 3
    @tanathos As in 3.5, when using identities, you have to wait for the `SaveChanges` call. If you want to use numeric ids without the node's tag, you can use `AsyncHiLoIdGenerator` and then override `GetDocumentIdFromId`, see https://github.com/ravendb/ravendb/blob/7823c5f1f0362072f1e051e38e3eb6fb76c9d06a/src/Raven.Client/Documents/Identity/AsyncHiLoIdGenerator.cs#L45 – Ayende Rahien Mar 13 '18 at 02:23
  • @AyendeRahien Thank you for the answer, I'll try this way so. Anyway is strange what you said, I'm certain in 3.5 after the Store() I already had my progressive Id (generated with the pattern "[collection]/"). My application would have not work at all otherwise. Is it possibly related to some configuration? – tanathos Mar 13 '18 at 08:11
  • I wrote the relevant code, if you used `collection/`, it was only available after `SaveChanges()`. – Ayende Rahien Mar 14 '18 at 08:50