---
title: "Session: How to Get Entity ID"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/get-entity-id"
version: "6.2"
language: "csharp"
---

# Session: How to Get Entity ID

> Client language: C#. Also available for [Java](https://docs.ravendb.net/6.2/client-api/session/how-to/get-entity-id.java.md), [Node.js](https://docs.ravendb.net/6.2/client-api/session/how-to/get-entity-id.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."

Entities does not have to contain an ID property or field. In the case of such an entity, and a need for knowing under what ID it is stored on the server, the `GetDocumentId` method was created.

## Syntax

```csharp
string GetDocumentId(object entity);
```

| Parameters | | |
| - | - | - |
| **entity** | object | Instance of an entity for which an ID will be returned |

| Return Value | |
| - | - |
| string | Returns the ID for a specified entity. The method may return `null` if `entity` is **null, isn't tracked, or the ID will be generated on the server**. |

## Example

```csharp
public class Comment
{
  public string Author { get; set; }

  public string Message { get; set; }
}
```

```csharp
string commentId = session
  .Advanced
  .GetDocumentId(comment); // e.g. comments/1-A
```

## See also

- [Working with Document Identifiers](https://docs.ravendb.net/6.2/client-api/document-identifiers/working-with-document-identifiers.md)
- [Global ID Generation Conventions](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/global.md)
- [Type-specific ID Generation Conventions](https://docs.ravendb.net/6.2/client-api/configuration/identifier-generation/type-specific.md)
- [Working with document identifiers](https://docs.ravendb.net/6.2/client-api/document-identifiers/working-with-document-identifiers.md)
- [Document Identifier Generation](https://docs.ravendb.net/6.2/server/kb/document-identifier-generation.md)
