RavenDB Retrieves the ID of the Newly Saved Document

I started playing with RavenDB (which really looks great so far). However, I was stuck trying to do the following.

I am storing a new document this way

Product p = new Product() { Title = "My Fancy Product" }; RavenSession.Store(p); 

Now I would like to get the id of a recently saved document. Who can do this? Just to access p.Id after the repository is down ...

Thanks in advance!

+4
source share
1 answer

The Id property of the Product class must be of type string instead of an integer.

Then you can get the auto-generated identifier after the operation:

 Product p = new Product() { Title = "My Fancy Product" }; RavenSession.Store(p); string id = p.Id; 

More information can be found in the documentation ( Document IDs ) Document IDs :

In the above example, we had the string Id property for BlogPost, and left it blank. This property will be used as the "primary key" for this document. Please note that RavenDB generated an identifier for us, "BlogPosts / 1", based on the default convention, which we will discuss in a second.

If the document does not have the Id property, RavenDB will still generate a unique identifier, but it will be available only by calling session.Advanced.GetDocumentId (object). In other words, having an Id property is completely optional, so you can explicitly define such a property only if you need this information to be more accessible.

+9
source

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


All Articles