GenericEFCoreHelper 2.0.0

dotnet add package GenericEFCoreHelper --version 2.0.0
                    
NuGet\Install-Package GenericEFCoreHelper -Version 2.0.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="GenericEFCoreHelper" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GenericEFCoreHelper" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="GenericEFCoreHelper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GenericEFCoreHelper --version 2.0.0
                    
#r "nuget: GenericEFCoreHelper, 2.0.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.
#:package GenericEFCoreHelper@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GenericEFCoreHelper&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=GenericEFCoreHelper&version=2.0.0
                    
Install as a Cake Tool

GenericEFCoreHelper

GenericEFCoreHelper is a helper library for Entity Framework Core.

Installation

You can install the package via NuGet Package Manager:

.NET CLI

dotnet add package GenericEFCoreHelper

Package Manager Console

Install-Package GenericEFCoreHelper

Usage

Service Registration

In your ServiceRegister.cs file, register the DbContext and repositories:

public static class ServiceRegister 
{ 
    public static void RegisterServices(this IServiceCollection services, IConfiguration configuration) 
    { 
        // Configure Entity Framework Core 
        var connectionString = configuration["ConnectionStrings:DemoWebAppConnectionString"] ?? throw new InvalidOperationException("Connection string 'DemoWebAppConnectionString' not found."); services.AddDbContext(options => options.UseSqlServer(connectionString), ServiceLifetime.Singleton);
        // Register Repositories
        services.AddGenericEFCHelperRepository();
        //Also we can register the lifetime of the instance
        //services.AddGenericEFCHelperRepository(ServiceLifetime.Scoped);

        services.AddTransient<IEmployeeService, EmployeeService>();
    }
}

Employee Service

In your EmployeeService.cs file, implement the IEmployeeService interface:

public class EmployeeService : IEmployeeService 
{ 
    private readonly IGenericRepository<Employee, ApplicationDbContext> _employeeRepository;
    public EmployeeService(IGenericRepository<Employee, ApplicationDbContext> employeeRepository)
    {
        _employeeRepository = employeeRepository;
    }

    public async Task<IEnumerable<Employee>> GetAllEmployeesAsync()
    {
        return await _employeeRepository.GetAllAsync();
    }

    public async Task<IEnumerable<Employee>> GetAllEmployeesAsync(
        Expression<Func<Employee, bool>>? predicate = null,
        Expression<Func<Employee, object>>? orderBy = null,
        bool ascending = true)
    {
        return await _employeeRepository.GetAllAsync(predicate, orderBy, ascending);
    }

    public async Task<IEnumerable<Employee>> GetAllEmployeesByIdAsync(int id)
    {
        return await _employeeRepository.GetAllByIdAsync(id);
    }

    public async Task<Employee?> GetEmployeeByIdAsync(int id)
    {
        return await _employeeRepository.GetByIdAsync(id);
    }

    public async Task<IEnumerable<Employee>> FindEmployeesAsync(Employee employee)
    {
        return await _employeeRepository.FindAsync(x => x == employee);
    }

    public async Task<bool> EmployeeExistsAsync(Employee employee)
    {
        return await _employeeRepository.ExistsAsync(x => x == employee);
    }

    public async Task<bool> AddEmployeeAsync(Employee employee)
    {
        return await _employeeRepository.AddAsync(employee);
    }

    public async Task<int> AddEmployeeAndReturnIdAsync(Employee employee)
    {
        return await _employeeRepository.AddAndReturnIdAsync(employee);
    }

    public async Task<bool> UpdateEmployeeAsync(Employee employee)
    {
        return await _employeeRepository.UpdateAsync(employee);
    }

    public async Task<bool> DeleteEmployeeAsync(int id)
    {
        return await _employeeRepository.DeleteAsync(id);
    }

    public async Task<DataTableResponse<Employee>> GetPagedEmployeesAsync(DataTableRequest request)
    {
        Expression<Func<Employee, bool>>? searchExpression = null;
        Expression<Func<Employee, object>>? orderExpression = null;

        // Apply search filter
        if (!string.IsNullOrEmpty(request.Search?.Value))
        {
            searchExpression = u => u.Name.Contains(request.Search.Value);
        }

        // Apply ordering
        if (request.Order != null && request.Order.Length > 0)
        {
            var order = request.Order[0];
            var column = request.Columns[order.Column]?.Data;

            if (!string.IsNullOrEmpty(column))
            {
                var parameter = Expression.Parameter(typeof(Employee), "u");
                var property = Expression.Property(parameter, column);
                orderExpression = Expression.Lambda<Func<Employee, object>>(Expression.Convert(property, typeof(object)), parameter);
            }
        }
        return await _employeeRepository.GetPagedDataAsync(request, searchExpression, orderExpression);
    }

    public async Task<Employee?> GetFirstOrDefaultEmployeeAsync(Expression<Func<Employee, bool>> predicate, params Expression<Func<Employee, object>>[] includes)
    {
        return await _employeeRepository.FirstOrDefaultAsync(predicate, includes);
    }

    public void RemoveEmployeesRange(IEnumerable<Employee> employees)
    {
        _employeeRepository.RemoveRange(employees);
    }

    public void RemoveEmployee(Employee employee)
    {
        _employeeRepository.Remove(employee);
    }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows 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
2.0.0 186 2/28/2025
1.0.4 163 2/28/2025
1.0.3 159 2/28/2025
1.0.2 186 2/22/2025
1.0.1 171 2/22/2025
1.0.0 170 2/20/2025