Fewbit.EntityFramework.CrudApi 1.0.0-beta-1

This is a prerelease version of Fewbit.EntityFramework.CrudApi.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Fewbit.EntityFramework.CrudApi --version 1.0.0-beta-1
NuGet\Install-Package Fewbit.EntityFramework.CrudApi -Version 1.0.0-beta-1
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="Fewbit.EntityFramework.CrudApi" Version="1.0.0-beta-1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fewbit.EntityFramework.CrudApi --version 1.0.0-beta-1
#r "nuget: Fewbit.EntityFramework.CrudApi, 1.0.0-beta-1"
#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 Fewbit.EntityFramework.CrudApi as a Cake Addin
#addin nuget:?package=Fewbit.EntityFramework.CrudApi&version=1.0.0-beta-1&prerelease

// Install Fewbit.EntityFramework.CrudApi as a Cake Tool
#tool nuget:?package=Fewbit.EntityFramework.CrudApi&version=1.0.0-beta-1&prerelease

EntityFramework.CrudApi

The "EntityFramework.CrudApi" project is a library that enables the easy exposure of an Entity Framework DbContext as a REST API, all with just a single line of code. Leveraging the capabilities of the Minimal API approach in DotNet Core, it uses reflection to dynamically create and expose REST resources (GET, PUT, POST, DELETE) for each Entity/Class mapped through Entity Framework. This approach streamlines the process of creating REST endpoints, requiring only a few lines of code.

Features

  • Minimal API Integration: The library seamlessly integrates with DotNet Core's Minimal API approach, providing a straightforward way to expose Entity Framework DbContext as RESTful endpoints.

  • Reflection-based Resource Creation: Through reflection, the library dynamically generates REST resources for each Entity/Class mapped using Entity Framework. This eliminates the need for manually defining endpoints, making the process more efficient.

  • Support for CRUD Operations: The generated API supports standard CRUD operations - GET, PUT, POST, and DELETE, allowing for easy manipulation of data stored in the underlying DbContext.

  • Simplified Endpoint Creation: With just a single line of code, developers can expose an entire DbContext through REST APIs, significantly reducing the amount of boilerplate code required.

Getting Started

Installation

To use the "EntityFramework.CrudApi" library, you can install it via NuGet Package Manager:

dotnet add package Fewbit.EntityFramework.CrudApi --version 1.0.0-alpha

Usage

  • Add a reference to the library in your project.

  • In your startup or configuration code, include the following line to expose your DbContext through REST APIs:

// Before build app
builder.Services.AddEntityFrameworkCrudApi(typeof(Program).Assembly);


// After app was built
app.MapCrudApiRoutes<MyDbContext>().WithOpenApi();
  • Replace MyDbContext with the name of your Entity Framework DbContext.

  • That's it! Your DbContext is now accessible through REST endpoints.

Example

var builder = WebApplication.CreateBuilder(args);

builder.Logging.AddConsole();

builder.Services.Configure<JsonOptions>(options =>
{
    options.SerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});

// Add DbContext
builder.Services.AddDbContext<MyDbContext>();

// Add Validators
builder.Services.AddValidatorsFromAssemblyContaining<Program>();

// Add AutoMapper
builder.Services.AddAutoMapper(typeof(Program));

// Add MediatR
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<Program>());

// Customize CrudApi

// Add CrudApi
builder.Services.AddEntityFrameworkCrudApi(typeof(Program).Assembly);

// Add Problems mapping for error output
builder.Services.AddProblemDetails();

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
    db.Database.EnsureCreated();
}

// ConfigureRouteHandlerBuilder the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
else
{
    app.UseExceptionHandler();
}

app.UseHttpsRedirection();

app.MapCrudApiRoutes<MovieStore>().WithOpenApi();

app.Run();
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-beta-3 49 4/23/2024
1.0.0-beta-2 45 4/22/2024
1.0.0-beta-1 77 1/19/2024
1.0.0-alpha-9 47 1/19/2024
1.0.0-alpha-8 49 1/19/2024
1.0.0-alpha-7 48 1/18/2024
1.0.0-alpha-6 57 1/17/2024
1.0.0-alpha-5 52 1/17/2024
1.0.0-alpha-4 58 1/16/2024
1.0.0-alpha-3 54 1/16/2024
1.0.0-alpha-2 54 1/16/2024
1.0.0-alpha-10 46 1/19/2024
1.0.0-alpha-1 54 1/15/2024
1.0.0-alpha 57 1/15/2024