Browse
EntityFrameworkRepository<TEntity>
publicnamespace Core.Data.Repositories;
public class EntityFrameworkRepository<TEntity> : IRepository<TEntity>
where TEntity : classA 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.
TEntity—The 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
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.
sender—The source of the event, typically the repository instance.
e—An 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
Properties
Static Methods
Creates a transient instance of the repository using the provided context.
context—The database context to use.
returns—A 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.
entity—The entity instance to be inserted.
cancellationToken—A token to observe for cancellation requests.
returns—The 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.
keyValues—The values of the primary key for the entity to be found.
cancellationToken—A token to observe for cancellation requests.
returns—The 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.
specification—The specification containing the query criteria to apply.
cancellationToken—A token to observe for cancellation requests.
returns—A 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.
entity—The 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.
entity—The added entity.