pub struct ChangeTracker { /* private fields */ }Expand description
Tracks changes to entities within a DbContext.
The single source of truth for EntityState, original EntitySnapshot,
modified_properties, and is_upsert across all tracked entities.
DbSet<T> holds the typed entity + entry_id; this tracker holds the
tracking state, joined by entry_id during save_changes.
Implementations§
Source§impl ChangeTracker
impl ChangeTracker
pub fn new() -> Self
Sourcepub fn track(
&mut self,
type_id: TypeId,
type_name: &str,
state: EntityState,
original: Option<EntitySnapshot>,
is_upsert: bool,
) -> u64
pub fn track( &mut self, type_id: TypeId, type_name: &str, state: EntityState, original: Option<EntitySnapshot>, is_upsert: bool, ) -> u64
Begins tracking an entity with the given state and optional original
snapshot. Returns the assigned entry_id for joining with DbSet.
Sourcepub fn detect_changes(&mut self, current_snapshots: &[(u64, EntitySnapshot)])
pub fn detect_changes(&mut self, current_snapshots: &[(u64, EntitySnapshot)])
Compares current property snapshots against stored originals.
For each Unchanged entry whose original exists, if the current
snapshot differs, transitions to Modified and records the changed
field names in modified_properties.
pub fn entry_state(&self, entry_id: u64) -> Option<EntityState>
pub fn entry_original(&self, entry_id: u64) -> Option<&EntitySnapshot>
pub fn entry_modified(&self, entry_id: u64) -> Option<&[String]>
pub fn entry_is_upsert(&self, entry_id: u64) -> bool
pub fn set_state(&mut self, entry_id: u64, state: EntityState)
pub fn set_modified(&mut self, entry_id: u64, modified: Vec<String>)
Sourcepub fn accept_all_changes(
&mut self,
current_snapshots: &[(u64, EntitySnapshot)],
)
pub fn accept_all_changes( &mut self, current_snapshots: &[(u64, EntitySnapshot)], )
After successful SaveChanges:
- Deleted entries are removed
- Added/Modified → Unchanged (original refreshed to current snapshot)
modified_propertiescleared
current_snapshots provides the post-save entity snapshots (with
backfilled auto-increment PKs) to become the new original.
Sourcepub fn reject_all_changes(&mut self)
pub fn reject_all_changes(&mut self)
Reverts all pending changes:
- Added entries are removed
- Modified/Deleted → Unchanged (original stays as-is)
modified_propertiescleared
Sourcepub fn clear_by_type(&mut self, type_id: TypeId)
pub fn clear_by_type(&mut self, type_id: TypeId)
Removes all tracked entries of the given type. Used by load_all to
clear stale tracking state before re-attaching fresh entities.
pub fn has_changes(&self) -> bool
pub fn clear(&mut self)
pub fn entries(&self) -> Vec<EntityEntry>
pub fn count_by_state(&self, state: EntityState) -> usize
pub fn entries_by_state(&self, state: EntityState) -> Vec<EntityEntry>
Sourcepub fn entry_views(&self) -> Vec<EntityEntryView>
pub fn entry_views(&self) -> Vec<EntityEntryView>
Returns (type_id, type_name, state) views for all tracked entries,
used to build SaveChangesContext for interceptors.