EntityFrameworkCore.Utils 1.0.0

dotnet add package EntityFrameworkCore.Utils --version 1.0.0                
NuGet\Install-Package EntityFrameworkCore.Utils -Version 1.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="EntityFrameworkCore.Utils" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.Utils --version 1.0.0                
#r "nuget: EntityFrameworkCore.Utils, 1.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.
// 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

Add Middleware

Overview

CRUDController

[!NOTE] The default Id type is Guid

  • 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 objects

  • DeleteAsync(TId id): delete one object by id.

  • DeleteAsync([FromBody] List<TId> ids): delete many objects by ids

  • SoftDeleteAsync(TId id): GPT: MAKE HERE DELCRIPTION OF THE METHOD

  • SoftDeleteAsync([FromBody] List<TId> id): GPT: MAKE HERE DELCRIPTION OF THE METHOD

CRUDService

[!NOTE] The default Id type is Guid

  • 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 objects

  • DeleteAsync(TId id): delete one object by id.

  • DeleteAsync([FromBody] List<TId> ids): delete many objects by ids

  • SoftDeleteAsync(TId id): GPT: MAKE HERE DELCRIPTION OF THE METHOD

  • SoftDeleteAsync([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 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. 
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.0.0 99 9/20/2024