Session: How to Evict Single Entity from a Session
We can clear all session operations and stop tracking of all entities by the using session.advanced.clear() method, but sometimes there is a need to only to do a cleanup for a single entity. For this purpose evict()
was introduced.
Syntax
session.advanced.evict(entity);
Parameters | ||
---|---|---|
entity | object | Entity that will be evicted |
Example I
const employee1 = new Employee();
employee1.firstName = "John";
employee1.lastName = "Doe";
const employee2 = new Employee();
employee2.firstName = "Joe";
employee2.lastName = "Shmoe";
await session.store(employee1);
await session.store(employee2);
session.advanced.evict(employee1);
await session.saveChanges(); // only 'Joe Shmoe' will be saved
Example II
let employee = await session.load("employees/1-A"); // loading from serer
employee = await session.load("employees/1-A"); // no server call
session.advanced.evict(employee);
employee = await session.load("employees/1-A"); // loading from server