Class SqlEntityFrameworkCrudRepositoryBase<TEntity, TDbContext>
Implementation of the repository pattern for an EF Core entity data type stored in SQL Server. Provides create, read, update, and delete logic.
Inheritance
Inherited Members
Namespace: Benday.EfCore .SqlServer
Assembly: Benday.EfCore.SqlServer.dll
Syntax
public abstract class SqlEntityFrameworkCrudRepositoryBase<TEntity, TDbContext> : SqlEntityFrameworkRepositoryBase<TEntity, TDbContext>, IDisposable, IRepository<TEntity> where TEntity : class, IEntityBase where TDbContext : DbContext
Type Parameters
Name | Description |
---|---|
TEntity | Entity data type managed by this repository implementation. Must be an instance of IInt32Identity. |
TDbContext | EF Core DbContext data type that manages this entity |
Constructors
| Edit this page View SourceSqlEntityFrameworkCrudRepositoryBase(TDbContext)
Constructor
Declaration
public SqlEntityFrameworkCrudRepositoryBase(TDbContext context)
Parameters
Type | Name | Description |
---|---|---|
TDb |
context | Instance of the EF Core DbContext to use for data operations |
Properties
| Edit this page View SourceEntityDbSet
References to the EF Core DbSet for data operation for this entity
Declaration
protected abstract DbSet<TEntity> EntityDbSet { get; }
Property Value
Type | Description |
---|---|
Db |
Includes
Names of entity properties that should be included during SELECT operations
Declaration
protected virtual List<string> Includes { get; }
Property Value
Methods
| Edit this page View SourceAddDefaultSort(IQueryable<TEntity>)
Template method to add sorting logic to SELECT queries. This method allows code to modify the query or replace the query before execution.
Declaration
protected virtual IQueryable<TEntity> AddDefaultSort(IQueryable<TEntity> query)
Parameters
Type | Name | Description |
---|---|---|
IQueryable<TEntity> | query | Proposed query for the entity |
Returns
Type | Description |
---|---|
IQueryable<TEntity> | Original or modified query to use for the query |
AddIncludes(IQueryable<TEntity>)
Populate eager loading includes for dependent objects
Declaration
protected virtual IQueryable<TEntity> AddIncludes(IQueryable<TEntity> queryable)
Parameters
Type | Name | Description |
---|---|---|
IQueryable<TEntity> | queryable | Proposed query for the entity |
Returns
Type | Description |
---|---|
IQueryable<TEntity> | Original or modified query to use for the query |
Exceptions
Type | Condition |
---|---|
Argument |
AfterDelete(TEntity)
Template method for adding logic after issuing the delete
Declaration
protected virtual void AfterDelete(TEntity deleteThis)
Parameters
Type | Name | Description |
---|---|---|
TEntity | deleteThis | Entity that is being deleted |
AfterSave(TEntity)
Template method for adding logic after saving. This implementation does nothing.
Declaration
protected virtual void AfterSave(TEntity saveThis)
Parameters
Type | Name | Description |
---|---|---|
TEntity | saveThis | Entity to save |
BeforeDelete(TEntity)
Template method for adding logic before issuing the delete
Declaration
protected virtual void BeforeDelete(TEntity deleteThis)
Parameters
Type | Name | Description |
---|---|---|
TEntity | deleteThis | Entity that is being deleted |
BeforeGetAll(IQueryable<TEntity>)
Template method for adding logic before issuing the GetAll command. This method allows code to modify the query or replace the query before execution.
Declaration
protected virtual IQueryable<TEntity> BeforeGetAll(IQueryable<TEntity> query)
Parameters
Type | Name | Description |
---|---|---|
IQueryable<TEntity> | query | Proposed query for the entity |
Returns
Type | Description |
---|---|
IQueryable<TEntity> | Original or modified query to use for the GetAll command |
BeforeSave(TEntity)
Template method for adding logic before saving. This implementation does nothing.
Declaration
protected virtual void BeforeSave(TEntity saveThis)
Parameters
Type | Name | Description |
---|---|---|
TEntity | saveThis | Entity to save |
BeforeSaveOnDependentEntities(TEntity)
Template method for performing dependent entity logic on the entity before saving. The default implementation calls GetDependentEntities() and calls BeforeSave() on any of those entities.
Declaration
protected virtual void BeforeSaveOnDependentEntities(TEntity saveThis)
Parameters
Type | Name | Description |
---|---|---|
TEntity | saveThis |
Delete(TEntity)
Delete an entity from the DbContext. This method checks if the entity has been detached from the DbContext instance and reattaches it if necessary before triggering a save on the DbContext.
Declaration
public virtual void Delete(TEntity deleteThis)
Parameters
Type | Name | Description |
---|---|---|
TEntity | deleteThis | Instance to delete |
Exceptions
Type | Condition |
---|---|
Argument |
GetAll()
Get all entities
Declaration
public virtual IList<TEntity> GetAll()
Returns
Type | Description |
---|---|
IList<TEntity> |
GetAll(int, bool)
Get all entities with options for limiting the number of returned records and skipping population of dependency relationships.
Declaration
public virtual IList<TEntity> GetAll(int maxNumberOfRows, bool noIncludes)
Parameters
Type | Name | Description |
---|---|---|
int | maxNumberOfRows | Maximum number of records to return |
bool | noIncludes | If true, skip populating of dependent entity relationships. This can be helpful for optimizing performance. |
Returns
Type | Description |
---|---|
IList<TEntity> |
GetById(int)
Retrieves a single matching entity with eager loading includes
Declaration
public virtual TEntity? GetById(int id)
Parameters
Type | Name | Description |
---|---|---|
int | id | Entity Id to retrieve |
Returns
Type | Description |
---|---|
TEntity | The matching entity or null if not found |
Save(TEntity)
Save an entity and its dependent children. This method verifies that the entities are attached to the DbContext. Dependent children are accessed via the IDependentEntityCollection interface. Template methods are available for customizing the logic before and after this save operation.
Declaration
public virtual void Save(TEntity saveThis)
Parameters
Type | Name | Description |
---|---|---|
TEntity | saveThis | Entity to save |
Exceptions
Type | Condition |
---|---|
Argument |