Adrian
Adrian

Reputation: 769

How do I automatically assign the id of saved entities?

Let's say I have the following structure of an entity I'm trying to save in a RavenDB v2.0 document store using the client side API.

public class Instance
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string Content { get; set; }
    }

If I'm creating 10-20 instances and I want to save the entity id for each entity respectively. What I can see in the documentation on the site is that you can retrieve the id of an entity after you call the session.SaveChanges() method, but that would mean that I need to call this method for each entity item that I have.

What's a best practice to use here? Is there any way that the client API would automatically save the entity id in a field of the entity class (is there an attribute for that maybe)?

Upvotes: 1

Views: 72

Answers (1)

Fitzchak Yitzchaki
Fitzchak Yitzchaki

Reputation: 9163

With the default conventions RavenDB set the ID value after calling session.Store(item), using an HiLo algorithm in order to generate the ID on the client without reaching to the server for each ID assignment. The ID is available on a property called "Id" can you can configure to look also for property named property.

Upvotes: 2

Related Questions