Browse
Sample.LibCore.Data.RepositoriesEntityFrameworkRepository
.md
class

EntityFrameworkRepository<TEntity>

public
namespace Core.Data.Repositories; public class EntityFrameworkRepository<TEntity> : IRepository<TEntity> where TEntity : class

A generic repository implementation using Entity Framework Core to perform standard CRUD operations. Abstracting the underlying DbContext, this class provides a foundational data access layer that can be easily inherited or used directly.

TEntityThe entity type managed by the repository.

// Example usage in a standard .NET Dependency Injection container var builder = WebApplication.CreateBuilder(args); builder.Services.AddScoped<IRepository<User>, EntityFrameworkRepository<User>>();

Constructors

#public EntityFrameworkRepository(DbContext dbContext)

Initializes a new instance of the EntityFrameworkRepository<TEntity> class.

dbContextThe database context to use.

throws ArgumentNullException: dbContext is null.

Delegates

#public delegate void EntityChangedEventHandler( object sender, EntityChangedEventArgs<TEntity> e );

Represents the method that will handle events triggered when an entity undergoes a lifecycle change.

senderThe source of the event, typically the repository instance.

eAn object that contains the event data.

Events

#public event EntityChangedEventHandler? EntityAdded;

Occurs when a new entity is successfully added to the repository.

This event is triggered synchronously after the Add operation is performed on the DbSet, but before SaveChanges is called.

Fields

#protected readonly DbContext _dbContext;

The underlying database context instance used for data access operations.

Derived classes can access this field directly to perform operations not exposed by the generic interface.

Properties

#public virtual DbContext Context { get; }

Gets the active database context for the current scope.

valueThe current DbContext instance injected during repository construction.

#public int MaxPageSize { get; set; }

Gets or sets the maximum page size used by ListAsync.

Static Methods

#public static EntityFrameworkRepository<TEntity> CreateTransient( DbContext context )

Creates a transient instance of the repository using the provided context.

contextThe database context to use.

returnsA new, non-dependency-injected instance of the repository.

Use this method sparingly; typically repositories should be resolved from the DI container.

Methods

#public async Task<TEntity> AddAsync( TEntity entity, CancellationToken cancellationToken = default )

Asynchronously adds a new entity to the underlying database context. The entity will be inserted into the database upon the next save operation.

entityThe entity instance to be inserted.

cancellationTokenA token to observe for cancellation requests.

returnsThe inserted entity with any database-generated values applied.

This method calls AddAsync on the underlying DbSet. It does not automatically call SaveChangesAsync.

#public async Task<TEntity?> GetByIdAsync( object[] keyValues, CancellationToken cancellationToken = default )

Asynchronously finds an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store.

keyValuesThe values of the primary key for the entity to be found.

cancellationTokenA token to observe for cancellation requests.

returnsThe entity found, or null if no entity is found.

#public Task<IReadOnlyList<TEntity>> ListAsync( ISpecification<TEntity> specification, CancellationToken cancellationToken = default )

Evaluates the given specification and returns the matching entities as a read-only list. Useful for complex queries encapsulating filtering, sorting, and pagination.

specificationThe specification containing the query criteria to apply.

cancellationTokenA token to observe for cancellation requests.

returnsA read-only list of entities that match the specification criteria.

Implementations should evaluate the specification criteria against the DbSet queryable.

#public void Update(TEntity entity)

Begins tracking the given entity and entries reachable from the given entity using the Modified state.

entityThe entity instance to update.

Since this operation only changes tracking state, it is synchronous. Changes are pushed to the database only when SaveChanges is called.

#protected virtual void OnAdded(TEntity entity)

Called after an entity has been added.

entityThe added entity.