Skip to main content

ChangeTracker

Struct ChangeTracker 

Source
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

Source

pub fn new() -> Self

Source

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.

Source

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.

Source

pub fn entry_state(&self, entry_id: u64) -> Option<EntityState>

Source

pub fn entry_original(&self, entry_id: u64) -> Option<&EntitySnapshot>

Source

pub fn entry_modified(&self, entry_id: u64) -> Option<&[String]>

Source

pub fn entry_is_upsert(&self, entry_id: u64) -> bool

Source

pub fn set_state(&mut self, entry_id: u64, state: EntityState)

Source

pub fn set_modified(&mut self, entry_id: u64, modified: Vec<String>)

Source

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_properties cleared

current_snapshots provides the post-save entity snapshots (with backfilled auto-increment PKs) to become the new original.

Source

pub fn reject_all_changes(&mut self)

Reverts all pending changes:

  • Added entries are removed
  • Modified/Deleted → Unchanged (original stays as-is)
  • modified_properties cleared
Source

pub fn detach(&mut self, entry_id: u64)

Detaches a specific entry by ID.

Source

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.

Source

pub fn has_changes(&self) -> bool

Source

pub fn clear(&mut self)

Source

pub fn entries(&self) -> Vec<EntityEntry>

Source

pub fn count_by_state(&self, state: EntityState) -> usize

Source

pub fn entries_by_state(&self, state: EntityState) -> Vec<EntityEntry>

Source

pub fn entry_views(&self) -> Vec<EntityEntryView>

Returns (type_id, type_name, state) views for all tracked entries, used to build SaveChangesContext for interceptors.

Source

pub fn is_auto_detect_changes_enabled(&self) -> bool

Source

pub fn set_auto_detect_changes(&mut self, enabled: bool)

Trait Implementations§

Source§

impl Debug for ChangeTracker

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ChangeTracker

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.