Useful extensions

For (SmartDb)Context and DbSet

DbContext extensions

OpenConnection(Async)

Opens and retains connection until end of scope. Call this method in long running processes to gain slightly faster database interaction.

FindTracked

Tries to locate an already loaded and tracked entity in the local state manager.

HasChanges

Checks whether at least one entity in the change tracker is in Added, Deleted or Modified state.

TryUpdate

Sets the state of an entity to Modified if it is detached.

TryChangeState

Changes the state of an entity object when requested state differs.

TryGetModifiedProperty

Determines whether an entity property has changed since it was attached.

GetModifiedProperties

Gets a list of modified properties for a given entity.

ReloadEntity(Async)

Reloads the entity from the database overwriting any property values with values from the database. The entity will be in the Unchanged state after calling this method.

DetachEntity

Detaches a single entity from the current context if it is attached.

DetachEntities

Detaches many entities from the current context.

IsCollectionLoaded

Checks whether a collection type navigation property has already been loaded for a given entity (either eagerly or lazily).

IsReferenceLoaded

Checks whether a reference type navigation property has already been loaded for a given entity (either eagerly or lazily).

LoadCollectionAsync

Loads entities referenced by a collection navigation property from database, unless data is already loaded.

LoadReferenceAsync

Loads an entity referenced by a navigation property from database, unless data is already loaded.

DbSet extensions

GetDbContext

Resolves the DbContext instance from which a given DbSet was obtained.

FindById(Async)

Finds an entity with a given id. If an entity with the given id is being tracked by the context, it is returned immediately without making a request to the database. Otherwise, a query is made to the database for an entity with the given id and this entity, if found, is returned. If no entity is found, then null is returned. If the tracked parameter is set to true, the entity is also attached to the context, so that subsequent calls can return the tracked entity without a database roundtrip.

GetMany(Async)

Loads many entities from database sorted by the given id sequence. Sort is applied in-memory.

DeleteAll(Async)

Truncates the table for a given entity type.

Last updated

Was this helpful?