Session: How to Evict Single Entity from a Session
We can clear all session operations and stop tracking of all entities by the using Clear method, but sometimes there is need to only to do a cleanup only for one entity. For this purpose Evict
was introduced.
Syntax
void Evict<T>(T entity);
Parameters | ||
---|---|---|
entity | T | Instance of an entity that will be evicted |
Example I
Employee employee1 = new Employee
{
FirstName = "John",
LastName = "Doe"
};
Employee employee2 = new Employee
{
FirstName = "Joe",
LastName = "Shmoe"
};
session.Store(employee1);
session.Store(employee2);
session.Advanced.Evict(employee1);
session.SaveChanges(); // only 'Joe Shmoe' will be saved
Example II
Employee employee = session.Load<Employee>("employees/1-A"); // loading from server
employee = session.Load<Employee>("employees/1-A"); // no server call
session.Advanced.Evict(employee);
employee = session.Load<Employee>("employees/1-A"); // loading from server