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.
source share