---
title: "Type-Specific Identifier Generation"
url: "https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/type-specific"
version: "6.2"
language: "csharp"
---

# Type-Specific Identifier Generation

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/type-specific.java.md), [Node.js](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/type-specific.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

\#Type-Specific Identifier Generation

[In the previous article](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/global.md), Global Identifier generation conventions were introduced. Any customization made by using those conventions changes the behavior for all stored entities.
Now we will show how to override the default ID generation in a more granular way, for particular types of entities.

To override default document identifier generation algorithms, you can register custom conventions per an entity type. You can include your own identifier generation logic.

```csharp
DocumentConventions RegisterAsyncIdConvention<TEntity>(Func<string, TEntity, Task<string>> func);
```

| Parameters | | |
| - | - | - |
| **func** | Func\<string, TEntity, Task\<string>> | Identifier generation function that supplies a result in async way for given database name (`string`) and entity object (`TEntity`). |

| Return Value | |
| - | - |
| DocumentConventions | Current `DocumentConventions` instance. |

> **Note**
>
> This method applied to both synchronous and asynchronous operations

> **Info:** Database name parameter {#database-name-parameter}
>
> The database name parameter is passed to the register convention methods to allow users to make Id generation decision per database.

### Example

Let's say that you want to use semantic identifiers for `Employee` objects. Instead of `employee/[identity]` you want to have identifiers like `employees/[lastName]/[firstName]`
(for the sake of simplicity, let us not consider the uniqueness of such identifiers). What you need to do is to create the convention that will combine the `employee` prefix, `LastName` and `FirstName` properties of an employee.

```csharp
store.Conventions.RegisterAsyncIdConvention<Employee>(
  (dbname, employee) =>
      Task.FromResult(string.Format("employees/{0}/{1}", employee.LastName, employee.FirstName)));
```

Now, when you store a new entity:

```csharp
using (var session = store.OpenSession())
{
  session.Store(new Employee
  {
      FirstName = "James",
      LastName = "Bond"
  });

  session.SaveChanges();
}
```

the client will associate the `employees/Bond/James` identifier with it.

## Inheritance

Registered conventions are inheritance-aware so all types that can be assigned from registered type will fall into that convention according to inheritance-hierarchy tree.

### Example

If we create a new class `EmployeeManager` that will derive from our `Employee` class and keep the convention registered in the last example, both types will use the following:

```csharp
using (var session = store.OpenSession())
{
  session.Store(new Employee // employees/Smith/Adam
  {
      FirstName = "Adam",
      LastName = "Smith"
  });

  session.Store(new EmployeeManager // employees/Jones/David
  {
      FirstName = "David",
      LastName = "Jones"
  });

  session.SaveChanges();
}
```

If we register two conventions, one for `Employee` and the second for `EmployeeManager` then they will be picked for their specific types.

```csharp
store.Conventions.RegisterAsyncIdConvention<Employee>(
  (dbname, employee) =>
      Task.FromResult(string.Format("employees/{0}/{1}", employee.LastName, employee.FirstName)));

store.Conventions.RegisterAsyncIdConvention<EmployeeManager>(
  (dbname, employee) =>
      Task.FromResult(string.Format("managers/{0}/{1}", employee.LastName, employee.FirstName)));

using (var session = store.OpenSession())
{
  session.Store(new Employee // employees/Smith/Adam
  {
      FirstName = "Adam",
      LastName = "Smith"
  });

  session.Store(new EmployeeManager // managers/Jones/David
  {
      FirstName = "David",
      LastName = "Jones"
  });

  session.SaveChanges();
}
```

## See also

- [Document identifier generation](https://docs.ravendb.net/6.2/server/kb/document-identifier-generation.md)
- [Working with document identifiers](https://docs.ravendb.net/6.2/client-api/document-identifiers/working-with-document-identifiers.md)
- [Global identifier generation conventions](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/global.md)
