A generic repository pattern for entity framework core that exposes CRUD functionality and async methods.
Install-Package Han.EntityFrameworkCore.Repository
- Fixed DataContext disposed exception
- Changed sort or queries to allow sorting in ascending and descending order
- Targeting latest net472 framework
-
Changed includes to support EntityFrameworks 'ThenInclude' and 'Include' eager loading. This requires a change to the
All
for repository.return All( predicate: predicate, includes: s => s.Include(i => i.Teachers).ThenInclude(i => i.Qualifications));
-
Updated to latest version of EntityFrameworkCore
Filters the DbSet based on a predicate, sorts in ascending order, skips a number of entities and returns the specified number of entities. Includes specifies which related entities to include in the query results.
Asynchronously filters the DbSet based on a predicate, sorts in ascending order, skips a number of entities and returns the specified number of entities. Includes specifies which related entities to include in the query results.
Determines whether any entities in the DbSet satisfy a condition.
Asynchronously determines whether any entities in the DbSet satisfy a condition.
Inserts the specified entities into the DbSet.
Asynchronously inserts the specified entities into the DbSet.
Removes the specified entities from the DbSet.
Asynchronously removes the specified entities from the DbSet.
Retrieves the first entity from the DbSet that satisfies the specified condition otherwise returns default value.
Asynchronously retrieves the first entity from the DbSet that satisfies the specified condition otherwise returns default value
Updates the specified entities in the DbSet.
Asynchronously updates the specified entities in the DbSet.
public class ApplicationDataContext : DbContext
{
public DbSet<School> Students { get; set; }
public DbSet<Teacher> Teachers { get; set; }
public DbSet<Qualification> Qualification { get; set; }
}
public class ApplicationRepository<T> : Repository<ApplicationDataContext, T>
where T : class
{
private readonly string _connection;
protected ApplicationRepository(string connection)
{
_connection = connection;
}
protected override ApplicationDataContext GetDataContext()
{
var options = new DbContextOptionsBuilder()
.UseSqlServer(_connection)
.Options;
return new ApplicationDataContext(options);
}
}
public interface ISchoolRepository
{
IEnumerable<School> GetSchools(Func<School, bool> predicate);
bool CreateSchool(School School);
bool UpdateSchool(School School);
bool DeleteSchool(School School);
}
public class SchoolRepository : ApplicationRepository<ApplicationDataContext>, ISchoolRepository
{
public ProductsRepository(string connection)
: base(connection)
{ }
public IEnumerable<School> GetSchools(Func<School, bool> predicate)
{
return All(
predicate: predicate,
includes: s => s.Include(i => i.Teachers).ThenInclude(i => i.Qualifications));
}
public bool CreateSchool(School School)
{
return Create(School);
}
public bool UpdateSchool(School School)
{
return Update(School);
}
public bool DeleteSchool(School School)
{
return Delete(School);
}
}
public interface ISchoolService
{
IEnumerable<School> GetSchoolByPostcode(string postcode);
}
public class SchoolService : ISchoolService
{
private readonly ISchoolRepository _repository;
public SchoolService(ISchoolRepository repository)
{
_repository = repository;
}
public IEnumerable<School> GetSchoolByPostcode(string lastname)
{
return _repository.GetSchools(p =>
p.Postcode.Equals(lastname, StringComparison.OrdinalIgnoreCase));
}
}
public class Startup
{
private void ConfigureRepositories(IServiceCollection services)
{
// Person
services
.AddSingleton<ISchoolRepository, SchoolRepository>()
.AddSingleton<ISchoolService, SchoolService>();
}
}