---
title: "How to Get Tracked Entities"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/get-tracked-entities"
version: "6.2"
language: "csharp"
---

# How to Get Tracked Entities

> Client language: C#. Also available for [Node.js](https://docs.ravendb.net/6.2/client-api/session/how-to/get-tracked-entities.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."

> **Note**
>
> - The Session [tracks all changes](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.md#tracking-changes)
>   made to all the entities it has either loaded, stored, deleted, or queried for,  
>   and persists to the server only what is needed when `SaveChanges()` is called.
>
> - You can use the session's advanced `GetTrackedEntities()` method  
>   to retrieve the **list of all entities tracked by the session**.
>
> - To check what is the actual type of change made to the entities, see:  
>   [Get entity changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-entity-has-changed.md#get-entity-changes), or
>   [Get session changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-there-are-any-changes-on-a-session.md#get-session-changes).
>
> - In this page:
>   - [Get tracked entities](#get-tracked-entities)
>   - [Syntax](#syntax)

## Get tracked entities

**Tracking stored entities**:

```csharp
using (var session = store.OpenSession())
{
  // Store entities within the session:
  Employee employee1 = new Employee {FirstName = "John", LastName = "Doe"};
  Employee employee2 = new Employee {FirstName = "David", LastName = "Brown"};
  Employee employee3 = new Employee {FirstName = "Tom", LastName = "Miller"};
  session.Store(employee1, "employees/1-A");
  session.Store(employee2, "employees/2-A");
  session.Store(employee3, "employees/3-A");

  // Get tracked entities:
  IDictionary<string, EntityInfo> trackedEntities = session.Advanced.GetTrackedEntities();

  // The session tracks the 3 new stored entities:
  Assert.Equal(3, trackedEntities.Keys.Count);
  var entityInfo = trackedEntities["employees/1-A"];
  Assert.Equal("employees/1-A", entityInfo.Id);
  Assert.True(entityInfo.Entity is Employee);
  
  // Save changes:
  session.SaveChanges();
  
  // The session keeps tracking the entities even after SaveChanges is called:
  trackedEntities = session.Advanced.GetTrackedEntities();
  Assert.Equal(3, trackedEntities.Keys.Count);
}
```

**Tracking loaded and deleted entities**:

```csharp
using (var session = store.OpenSession())
{
  // Load entity:
  Employee employee1 = session.Load<Employee>("employees/1-A");
  
  // Delete entity:
  session.Delete("employees/3-A");
  
  // Get tracked entities:
  IDictionary<string, EntityInfo> trackedEntities = session.Advanced.GetTrackedEntities();
  
  // The session tracks the 2 entities:
  Assert.Equal(2, trackedEntities.Keys.Count);
  
  // Note the 'IsDeleted' property that is set for deleted entities:
  var entityInfo = trackedEntities["employees/3-A"];
  Assert.True(entityInfo.IsDeleted);
  
  // Save changes:
  session.SaveChanges();
}
```

**Tracking queried entities**:

```csharp
using (var session = store.OpenSession())
{
  // Query for all employees:
  var employees = session.Query<Employee>().ToList();
  
  // Get tracked entities:
  IDictionary<string, EntityInfo> trackedEntities = session.Advanced.GetTrackedEntities();
  
  // The session tracks the entities loaded via the query:
  Assert.Equal(2, trackedEntities.Keys.Count); 
}
```

## Syntax

```csharp
IDictionary<string, EntityInfo> GetTrackedEntities();
```

```csharp
public class EntityInfo
{
  public string Id { get; set; }
  public object Entity { get; set; }
  public bool IsDeleted { get; set; }
}
```

## See also

- [What is a session and how does it work](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.md)
- [How to check for entity changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-entity-has-changed.md)
- [Evict entity from session](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.md)
- [Refresh entity](https://docs.ravendb.net/6.2/client-api/session/how-to/refresh-entity.md)
