Session: How to Evict Single Entity from a Session
We can clear all session operations and stop tracking of all entities using the
session.advanced.clear() method,
there's a need to do cleanup only for one entity. This is what evict()
is for.
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