pub struct ChangeTracker { /* private fields */ }Expand description
Tracks changes to entities within a DbContext.
Implementations§
Source§impl ChangeTracker
impl ChangeTracker
pub fn new() -> Self
Sourcepub fn track_entity_with_snapshot(
&mut self,
type_id: TypeId,
type_name: &str,
state: EntityState,
snapshot: HashMap<String, String>,
) -> u64
pub fn track_entity_with_snapshot( &mut self, type_id: TypeId, type_name: &str, state: EntityState, snapshot: HashMap<String, String>, ) -> u64
Takes a snapshot of the entity’s current properties and begins tracking.
The snapshotter closure should return a map of property-name �?string
serialization of the property’s current value.
Sourcepub fn track_entity(
&mut self,
type_id: TypeId,
type_name: &str,
state: EntityState,
) -> u64
pub fn track_entity( &mut self, type_id: TypeId, type_name: &str, state: EntityState, ) -> u64
Begins tracking an entity without taking a snapshot (used for Added entities that have no pre-existing state).
Sourcepub fn detect_changes_with_properties(
&mut self,
current_properties: &[(u64, HashMap<String, String>)],
)
pub fn detect_changes_with_properties( &mut self, current_properties: &[(u64, HashMap<String, String>)], )
Compares current property values (provided by the caller) against the
stored snapshots. Any property whose value differs is marked as modified,
and the entity transitions to EntityState::Modified.
Sourcepub fn update_snapshot(
&mut self,
entry_id: u64,
properties: HashMap<String, String>,
)
pub fn update_snapshot( &mut self, entry_id: u64, properties: HashMap<String, String>, )
Marks the property snapshot for the given entry as updated. Used after a successful SaveChanges to update the “original” values.
Sourcepub fn has_changes(&self) -> bool
pub fn has_changes(&self) -> bool
Returns whether any tracked entity has changes pending.
Sourcepub fn entries(&self) -> Vec<EntityEntry>
pub fn entries(&self) -> Vec<EntityEntry>
Returns an iterator over tracked entry views.
Sourcepub fn count_by_state(&self, state: EntityState) -> usize
pub fn count_by_state(&self, state: EntityState) -> usize
Returns count of entities in a given state.
Sourcepub fn entries_by_state(&self, state: EntityState) -> Vec<EntityEntry>
pub fn entries_by_state(&self, state: EntityState) -> Vec<EntityEntry>
Returns entities grouped by their state.
Sourcepub fn accept_all_changes(&mut self)
pub fn accept_all_changes(&mut self)
After successful SaveChanges:
- Added �?Unchanged (save snapshot)
- Modified �?Unchanged (save current as new snapshot)
- Deleted �?Detached (removed from tracker)
Sourcepub fn reject_all_changes(&mut self)
pub fn reject_all_changes(&mut self)
Reverts all pending changes (detached state reverted).