Increment Next Identity Operation


  • Use NextIdentityForOperation to increment the latest identity value set on the server for the specified collection in the database.

  • The next document that will be created using an identity for the collection will receive the consecutive integer value.

  • In this page:


Increment the next identity value

// Create a document with an identity ID:
// ======================================

using (var session = store.OpenSession())
{ 
    // Pass a collection name that ends with a pipe '|' to create an identity ID
    session.Store(new Company { Name = "RavenDB" }, "companies|");
    session.SaveChanges();
    // => Document "companies/1" will be created 
}

// Increment the identity value on the server:
// ===========================================

// Define the next identity operation
// Pass the collection name (can be with or without a pipe)
var nextIdentityOp = new NextIdentityForOperation("companies|");

// Execute the operation by passing it to Maintenance.Send
// The latest value will be incremented to "2"
// and the next document created with an identity will be assigned "3"
long incrementedValue = store.Maintenance.Send(nextIdentityOp);

// Create another document with an identity ID:
// ============================================

using (var session = store.OpenSession())
{ 
    session.Store(new Company { Name = "RavenDB" }, "companies|");
    session.SaveChanges();
    // => Document "companies/3" will be created
}
// Create a document with an identity ID:
// ======================================

using (var asyncSession = store.OpenAsyncSession())
{ 
    // Pass a collection name that ends with a pipe '|' to create an identity ID
    asyncSession.StoreAsync(new Company { Name = "RavenDB" }, "companies|");
    asyncSession.SaveChangesAsync(); 
    // => Document "companies/1" will be created 
}

// Increment the identity value on the server:
// ===========================================

// Define the next identity operation
// Pass the collection name (can be with or without a pipe)
var nextIdentityOp = new NextIdentityForOperation("companies|");

// Execute the operation by passing it to Maintenance.SendAsync
// The latest value will be incremented to "2"
// and the next document created with an identity will be assigned "3"
long incrementedValue = await store.Maintenance.SendAsync(nextIdentityOp);

// Create another document with an identity ID:
// ============================================

using (var asyncSession = store.OpenAsyncSession())
{ 
    asyncSession.StoreAsync(new Company { Name = "AnotherCompany" }, "companies|");
    asyncSession.SaveChangesAsync();
    // => Document "companies/3" will be created
}

Syntax

public NextIdentityForOperation(string name);
Parameter Type Description
name string The collection name for which to increment the identity value.
Can be with or without a pipe in the end (e.g. "companies" or "companies|".