2
votes

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?

1

1 Answers

3
votes

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.