Seed Identity Operation



Set a higher identity value

You can replace the latest identity value on the server with a new, higher number.

// Seed a higher identity value on the server:
// ===========================================

// Define the seed identity operation. Pass:
//   * The collection name (can be with or without a pipe)
//   * The new value to set
var seedIdentityOp = new SeedIdentityForOperation("companies|", 23);

// Execute the operation by passing it to Maintenance.Send
// The latest value on the server will be incremented to "23"
// and the next document created with an identity will be assigned "24"
long seededValue = store.Maintenance.Send(seedIdentityOp);

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

using (var session = store.OpenSession())
{ 
    session.Store(new Company { Name = "RavenDB" }, "companies|");
    session.SaveChanges();
    // => Document "companies/24" will be created
}
// Seed the identity value on the server:
// ======================================

// Define the seed identity operation. Pass:
//   * The collection name (can be with or without a pipe)
//   * The new value to set
var seedIdentityOp = new SeedIdentityForOperation("companies|", 23);

// Execute the operation by passing it to Maintenance.SendAsync
// The latest value on the server will be incremented to "23"
// and the next document created with an identity will be assigned "24"
long seededValue = await store.Maintenance.SendAsync(seedIdentityOp);

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

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

Force a smaller identity value

  • You can set the latest identity value to a number that is smaller than the current latest value.

  • Before proceeding, first ensure that documents with an identity value higher than the new number do not exist.

// Force a smaller identity value on the server:
// =============================================

// Define the seed identity operation. Pass:
//   * The collection name (can be with or without a pipe)
//   * The new value to set
//   * Set 'forceUpdate' to true
var seedIdentityOp = new SeedIdentityForOperation("companies|", 5, forceUpdate: true);

// Execute the operation by passing it to Maintenance.Send
// The latest value on the server will be decremented to "5"
// and the next document created with an identity will be assigned "6"
long seededValue = store.Maintenance.Send(seedIdentityOp);

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

using (var session = store.OpenSession())
{ 
    session.Store(new Company { Name = "RavenDB" }, "companies|");
    session.SaveChanges();
    // => Document "companies/6" will be created
}
// Force a smaller identity value on the server:
// =============================================

// Define the seed identity operation. Pass:
//   * The collection name (can be with or without a pipe)
//   * The new value to set
//   * Set 'forceUpdate' to true
var seedIdentityOp = new SeedIdentityForOperation("companies|", 5, forceUpdate: true);

// Execute the operation by passing it to Maintenance.SendAsync
// The latest value on the server will be decremented to "5"
// and the next document created with an identity will be assigned "6"
long seededValue = await store.Maintenance.SendAsync(seedIdentityOp);

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

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

Syntax

public SeedIdentityForOperation(string name, long value, bool forceUpdate = false);
Parameter Type Description
name string The collection name for which to seed the identity value.
Can be with or without a pipe in the end (e.g. "companies" or "companies|".
value long The number to set as the latest identity value.
forceUpdate bool true - force a new value that is smaller than the latest.
false - only a higher value can be set.