GenericRepository.EFCore 2.0.4

dotnet add package GenericRepository.EFCore --version 2.0.4
                    
NuGet\Install-Package GenericRepository.EFCore -Version 2.0.4
                    
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="GenericRepository.EFCore" Version="2.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GenericRepository.EFCore" Version="2.0.4" />
                    
Directory.Packages.props
<PackageReference Include="GenericRepository.EFCore" />
                    
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 GenericRepository.EFCore --version 2.0.4
                    
#r "nuget: GenericRepository.EFCore, 2.0.4"
                    
#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.
#addin nuget:?package=GenericRepository.EFCore&version=2.0.4
                    
Install as a Cake Addin
#tool nuget:?package=GenericRepository.EFCore&version=2.0.4
                    
Install as a Cake Tool

GenericRepository

  • This is a generic repository of basic CRUD operation
  • In this read entities with expression filters, sorting and include other dependent entities
  • It also return a queryable entity on which you can apply other linq extensions
  • In this new version has introduced IAuditable, Auditable and PagedList objects for the include auditable fields and paging.

How to Use

Method : 1

public class UnitOfWork(AppDbContext context) : UnitOfWork<AppDbContext>(context)
{
}

In Program.cs

services.AddTransient<IUnitOfWork, UnitOfWork>();

Method : 2

public interface IProductRepository : IRepository<Product>
{
    bool Status();
}

public class ProductRepository(TestDbContext context)
    : Repository<Product, TestDbContext>(context), IProductRepository
{
    public bool Status()
        => true;
}
public class UnitOfWork : IUnitOfWork
{
    private readonly AppDbContext _context;

    public UnitOfWork(AppDbContext context)
    {
        _context = context;

        Categories = new Repository<Category, AppDbContext>(_context);
        Products = new ProductRepository(_context);
    }

    public IRepository<Category> Categories { get; private set; }
    public IRepository<Product> Products { get; private set; }


    public void Dispose()
        => _context.Dispose();

    public async Task<int> SaveChangesAsync()
        => await _context.SaveChangesAsync();
}

In Endpoint OR Controller

app.MapGet("/api/products", async (IUnitOfWork repo) =>
{
    var products = await repo.Of<Product>().RecordsAsync();
    return Results.Ok(products);
});

app.MapGet("/api/products/{id:int}", async (int id, IUnitOfWork repo) =>
{
    var products = await repo.Of<Product>().FindByIdAsync(id);
    return Results.Ok(products);
});

app.MapGet("/api/products/{id:int}", async (int id, IUnitOfWork repo) =>
{
    var products = await repo.Of<Product>().FirstOrDefaultAsync(x=>x.Id == id);
    return Results.Ok(products);
});

app.MapGet("/api/products/{name}", async (string name, IUnitOfWork repo) =>
{
    var products = await repo.Of<Product>().FindByNameAsync(name);
    return Results.Ok(products);
});

app.MapGet("/api/products/{name}", async (string name, IUnitOfWork repo) =>
{
    var products = await repo.Of<Product>().FirstOrDefaultAsync(x=>x.Name == name);
    return Results.Ok(products);
});


app.MapPost("/api/product/create", async (IUnitOfWork repo, Product product) =>
{
    await repo.Of<Product>().AddAsync(product);
    await repo.SaveChangesAsync()

    return Results.Ok(new { Product = product, Status = "Success" });
});

app.MapPost("/api/product/{id:int}", async (int id, IUnitOfWork repo) =>
{
    var product = await repo.Of<Product>().Entity()
                                    .Include(x=>x.Category)
                                    .Where(x=>x.Id == id)
                                    .FirstOrDefaultAsync();

    var products = await repo.Of<Product>().Entity()
                                    .Include(x=>x.Category)
                                    .Where(x=>x.Id == id)
                                    .ToListAsync();

    return Results.Ok(new { Product = product, Status = "Success" });
});
``
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.4 65 6/7/2025
2.0.2 136 9/7/2024
2.0.1 118 9/7/2024
2.0.0 149 3/28/2024
1.0.6 128 3/26/2024
1.0.5 117 3/26/2024
1.0.2 149 3/19/2024
1.0.1 130 3/18/2024
1.0.0 135 3/18/2024