Han.EntityFrameworkCore.Repository 1.3.0

dotnet add package Han.EntityFrameworkCore.Repository --version 1.3.0
NuGet\Install-Package Han.EntityFrameworkCore.Repository -Version 1.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Han.EntityFrameworkCore.Repository" Version="1.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Han.EntityFrameworkCore.Repository --version 1.3.0
#r "nuget: Han.EntityFrameworkCore.Repository, 1.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Han.EntityFrameworkCore.Repository as a Cake Addin
#addin nuget:?package=Han.EntityFrameworkCore.Repository&version=1.3.0

// Install Han.EntityFrameworkCore.Repository as a Cake Tool
#tool nuget:?package=Han.EntityFrameworkCore.Repository&version=1.3.0

Han.EntityFrameworkCore.Repository

A generic repository pattern for entity framework core that exposes CRUD functionality and async methods.

Installation

Install-Package Han.EntityFrameworkCore.Repository

Changes

Version 1.3

  • Fixed DataContext disposed exception
  • Changed sort or queries to allow sorting in ascending and descending order
  • Targeting latest net472 framework

Version 1.2

  • 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

Documentation

All

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.

AllAsync

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.

Any

Determines whether any entities in the DbSet satisfy a condition.

AnyAsync

Asynchronously determines whether any entities in the DbSet satisfy a condition.

Create

Inserts the specified entities into the DbSet.

CreateAsync

Asynchronously inserts the specified entities into the DbSet.

Delete

Removes the specified entities from the DbSet.

DeleteAsync

Asynchronously removes the specified entities from the DbSet.

Get

Retrieves the first entity from the DbSet that satisfies the specified condition otherwise returns default value.

GetAsync

Asynchronously retrieves the first entity from the DbSet that satisfies the specified condition otherwise returns default value

Update

Updates the specified entities in the DbSet.

UpdateAsync

Asynchronously updates the specified entities in the DbSet.

Usage

Application Database:

    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);
        }
	}

Repository:

    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);
        }
    }

Service:

    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));
        }
    }

Dependency Injection - IoC:

    public class Startup
    {
        private void ConfigureRepositories(IServiceCollection services)
        {
            // Person
            services
                .AddSingleton<ISchoolRepository, SchoolRepository>()
                .AddSingleton<ISchoolService, SchoolService>();
        }
    }
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 619 3/14/2019