EntityFrameworkCore.Utils
1.0.0
dotnet add package EntityFrameworkCore.Utils --version 1.0.0
NuGet\Install-Package EntityFrameworkCore.Utils -Version 1.0.0
<PackageReference Include="EntityFrameworkCore.Utils" Version="1.0.0" />
paket add EntityFrameworkCore.Utils --version 1.0.0
#r "nuget: EntityFrameworkCore.Utils, 1.0.0"
// Install EntityFrameworkCore.Utils as a Cake Addin #addin nuget:?package=EntityFrameworkCore.Utils&version=1.0.0 // Install EntityFrameworkCore.Utils as a Cake Tool #tool nuget:?package=EntityFrameworkCore.Utils&version=1.0.0
Documentation
[!IMPORTANT] Make sure that
Middleware
has been added
Overview
CRUDController
[!NOTE] The default
Id
type isGuid
GetAsync()
: get all data.GetByIdAsync(TId id)
: get one object by id.ExecuteSqlAsync([FromBody] ExecuteSqlRequestDto obj)
: run sql command.InsertAsync([FromBody] TRequestDto obj)
: insert one object.InsertAsync([FromBody] List<TRequestDto> obj)
: insert many objects.UpdateAsync([FromBody] TRequestDto obj)
: update one object.UpdateAsync([FromBody] List<TRequestDto> obj)
: update many objectsDeleteAsync(TId id)
: delete one object by id.DeleteAsync([FromBody] List<TId> ids)
: delete many objects by idsSoftDeleteAsync(TId id)
: GPT: MAKE HERE DELCRIPTION OF THE METHODSoftDeleteAsync([FromBody] List<TId> id)
: GPT: MAKE HERE DELCRIPTION OF THE METHOD
CRUDService
[!NOTE] The default
Id
type isGuid
GetAsync()
: get all data.GetByIdAsync(TId id)
: get one object by id.ExecuteSqlAsync([FromBody] ExecuteSqlRequestDto obj)
: run sql command.InsertAsync([FromBody] TRequestDto obj)
: insert one object.InsertAsync([FromBody] List<TRequestDto> obj)
: insert many objects.UpdateAsync([FromBody] TRequestDto obj)
: update one object.UpdateAsync([FromBody] List<TRequestDto> obj)
: update many objectsDeleteAsync(TId id)
: delete one object by id.DeleteAsync([FromBody] List<TId> ids)
: delete many objects by idsSoftDeleteAsync(TId id)
: GPT: MAKE HERE DELCRIPTION OF THE METHODSoftDeleteAsync([FromBody] List<TId> id)
: GPT: MAKE HERE DELCRIPTION OF THE METHOD
How to add EntityFrameworkCore.Utils
to the project
Here’s a step-by-step guide on how to add EntityFrameworkCore.Utils
installation
dotnet add package Clouds.Net.AWS
Example Usage
CRUDController
Controllers/YOUR_CONTOLLER_NAME
using Utils.EF.Controllers;
using Utils.EF.Interfaces;
namespace PROJECT_NAME.API.Controllers
{
public class YOUR_CONTOLLER_NAME : CRUDController<RESPONSE_TYPE, REQUEST_TYPE>
{
public YOUR_CONTOLLER_NAME(ICRUDService<RESPONSE_TYPE, REQUEST_TYPE> service) : base(service)
{
}
// Here is your code
}
}
Also, you can use your Id type
using Utils.EF.Controllers;
using Utils.EF.Interfaces;
namespace PROJECT_NAME.API.Controllers
{
public class YOUR_CONTOLLER_NAME : CRUDController<YOUR_ID_TYPE, RESPONSE_TYPE, REQUEST_TYPE>
{
public YOUR_CONTOLLER_NAME(ICRUDService<YOUR_ID_TYPE, RESPONSE_TYPE, REQUEST_TYPE> service) : base(service)
{
}
// Here is your code
}
}
CRUDService
There're protected fields
_mapper
AutoMapper_context
Your database context_table
the table which has been found for your TModel type. You can use it to make CRUD operation in your service
using Utils.EF.Interfaces;
namespace PROJECT_NAME
{
public static class DI
{
public static IServiceCollection AddServices(this IServiceCollection services)
{
services.AddTransient<ICRUDService<RESPONSE_TYPE, REQUEST_TYPE>, CRUDService<RESPONSE_TYPE, REQUEST_TYPE>>();
// Here are other injections
return services;
}
}
}
How to add the CRUDService in DI
DI.cs
Also, you can use your Id type
using Utils.EF.Interfaces;
using Utils.EF.Services;
namespace PROJECT_NAME
{
public static class DI
{
public static IServiceCollection AddServices(this IServiceCollection services)
{
services.AddTransient<ICRUDService<YOUR_ID_TYPE, RESPONSE_TYPE, REQUEST_TYPE>, CRUDService<YOUR_ID_TYPE, RESPONSE_TYPE, REQUEST_TYPE>>();
// Here are other injections
return services;
}
}
}
How to create your own service based on the CRUDService
Services/YOUR_SERVICE_NAME.cs
using Utils.EF.Services;
namespace PROJECT_NAME
{
public class Service : CRUDService<RESPONSE_TYPE, REQUEST_TYPE>
{
public Service(IMapper mapper, DB_CONTEXT context) : base(mapper, context)
{
}
// Here is your code
}
}
Also, you can use your Id type
using Utils.EF.Services;
namespace PROJECT_NAME
{
public class Service : CRUDService<YOUR_ID_TYPE, RESPONSE_TYPE, REQUEST_TYPE>
{
public Service(IMapper mapper, DB_CONTEXT context) : base(mapper, context)
{
}
// Here is your code
}
}
Add Middleware
into API
Middleware/ErrorHandlerMiddleware.cs
using System.Net;
namespace PROJECT_NAME.API.Middleware
{
public class ErrorHandlerMiddleware
{
private readonly RequestDelegate _next;
public ErrorHandlerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex);
}
}
private static async Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var result = System.Text.Json.JsonSerializer.Serialize(new
{
error = new
{
message = "An unexpected error occurred.",
detailed = exception.Message
}
});
await context.Response.WriteAsync(result);
}
}
}
Use ErrorHandlerMiddleware
Program.cs
app.UseMiddleware<ErrorHandlerMiddleware>();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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 is compatible. 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. |
-
net7.0
- AutoMapper (>= 12.0.1)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.EntityFrameworkCore (>= 7.0.5)
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Data.Common (>= 4.3.0)
-
net8.0
- AutoMapper (>= 12.0.1)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.EntityFrameworkCore (>= 7.0.5)
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.5)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.1)
- System.Data.Common (>= 4.3.0)
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.0.0 | 95 | 9/20/2024 |