Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/craigmccauley/queryr.entityframeworkcore
Ad-Hoc querying library for EntityFrameworkCore
https://github.com/craigmccauley/queryr.entityframeworkcore
csharp csharp-library entity-framework-core query-builder
Last synced: 3 months ago
JSON representation
Ad-Hoc querying library for EntityFrameworkCore
- Host: GitHub
- URL: https://github.com/craigmccauley/queryr.entityframeworkcore
- Owner: craigmccauley
- License: mit
- Created: 2023-01-25T20:29:38.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-02T12:58:02.000Z (8 months ago)
- Last Synced: 2024-10-08T01:18:33.991Z (4 months ago)
- Topics: csharp, csharp-library, entity-framework-core, query-builder
- Language: C#
- Homepage: https://www.nuget.org/packages/QueryR.EntityFrameworkCore
- Size: 1.5 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# QueryR.EntityFrameworkCore
![QueryR Logo](./assets/logo.png)
[![.NET](https://github.com/craigmccauley/QueryR.EntityFrameworkCore/actions/workflows/dotnet.yml/badge.svg)](https://github.com/craigmccauley/QueryR.EntityFrameworkCore/actions/workflows/dotnet.yml)
QueryR.EntityFrameworkCore adds a few important items into [QueryR](https://github.com/craigmccauley/QueryR) for use with EntityFrameworkCore.
## Includes
QueryR.EntityFrameworkCore introduces a new `EfQuery` object that should be used for querying instead of the QueryR `Query` object.
The `EfQuery` object has an `Includes` member where the navigation property path can be set to include related entities with the query. It is important to use this setting for specifying Include statements as the values control the maximum search depth for the SparseFields.
## Async QueryResult Extensions
QueryR.EntityFrameworkCore would not be complete without new QueryResult extension methods to call the `CountAsync` and `ToListAsync` extension methods provided by EntityFrameworkCore.
## Web Api Example
```CSharp
public class GetAll(IGetAllKerbalsService getAllKerbalsService) : ControllerBase
{
[HttpGet(Routes.Api.Kerbals.Url)]
public async Task Action(
[FromQuery(Name = "")] QueryParameters parameters) =>
Ok(await getAllKerbalsService.GetAllAsync(parameters));public class ListWithTotalCount
{
public int TotalCount { get; set; }
public List Items { get; set; } = new();
}
public interface IGetAllKerbalsService
{
Task> GetAllAsync(QueryParameters parameters, CancellationToken cancellationToken = default);
}
public class GetAllKerbalsService(
IQueryParametersMapper queryParametersMapper,
KerbalDbContext kerbalDbContext
) : IGetAllKerbalsService
{
public readonly IQueryParametersMapper queryParametersMapper = queryParametersMapper;
public readonly KerbalDbContext kerbalDbContext = kerbalDbContext;public async Task> GetAllAsync(QueryParameters parameters, CancellationToken cancellationToken = default)
{
kerbalDbContext.Database.EnsureCreated();var query = queryParametersMapper.ToQuery(parameters);
var (totalCount, kerbals) = await kerbalDbContext.Set().Query(query)
.GetCountAndListAsync(cancellationToken);//Normally you would use a DTO mapper here to prevent cyclic dependencies.
//The ExampleRequests.http file has a request that uses sparse fields to avoid this for the purposes of the demo.
return new ListWithTotalCount
{
TotalCount = totalCount,
Items = kerbals
};
}
}
}```