Show / Hide Table of Contents

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
object
SqlEntityFrameworkRepositoryBase<TEntity, TDbContext>
SqlEntityFrameworkCrudRepositoryBase<TEntity, TDbContext>
SqlEntityFrameworkSearchableRepositoryBase<TEntity, TDbContext>
Implements
IDisposable
IRepository<TEntity>
Inherited Members
SqlEntityFrameworkRepositoryBase<TEntity, TDbContext>.Dispose()
SqlEntityFrameworkRepositoryBase<TEntity, TDbContext>.Dispose(bool)
SqlEntityFrameworkRepositoryBase<TEntity, TDbContext>.Context
SqlEntityFrameworkRepositoryBase<TEntity, TDbContext>.VerifyItemIsAddedOrAttachedToDbSet(DbSet<TEntity>, TEntity)
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
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 Source

SqlEntityFrameworkCrudRepositoryBase(TDbContext)

Constructor

Declaration
public SqlEntityFrameworkCrudRepositoryBase(TDbContext context)
Parameters
Type Name Description
TDbContext context

Instance of the EF Core DbContext to use for data operations

Properties

| Edit this page View Source

EntityDbSet

References to the EF Core DbSet for data operation for this entity

Declaration
protected abstract DbSet<TEntity> EntityDbSet { get; }
Property Value
Type Description
DbSet<TEntity>
| Edit this page View Source

Includes

Names of entity properties that should be included during SELECT operations

Declaration
protected virtual List<string> Includes { get; }
Property Value
Type Description
List<string>

Methods

| Edit this page View Source

AddDefaultSort(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

| Edit this page View Source

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
ArgumentNullException
| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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

| Edit this page View Source

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
| Edit this page View Source

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
ArgumentNullException
| Edit this page View Source

GetAll()

Get all entities

Declaration
public virtual IList<TEntity> GetAll()
Returns
Type Description
IList<TEntity>
| Edit this page View Source

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>
| Edit this page View Source

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

| Edit this page View Source

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
ArgumentNullException

Implements

IDisposable
IRepository<T>
  • Edit this page
  • View Source
In this article
Back to top Copyright © www.benday.com | info@benday.com