EntityModelBinder 1.0.2

dotnet add package EntityModelBinder --version 1.0.2
                    
NuGet\Install-Package EntityModelBinder -Version 1.0.2
                    
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="EntityModelBinder" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EntityModelBinder" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="EntityModelBinder" />
                    
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 EntityModelBinder --version 1.0.2
                    
#r "nuget: EntityModelBinder, 1.0.2"
                    
#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.
#:package EntityModelBinder@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EntityModelBinder&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=EntityModelBinder&version=1.0.2
                    
Install as a Cake Tool

Route Entity Model Binder 🚀

A Laravel-inspired Route Model Binder for ASP.NET Core. Automatically resolves database entities from route parameters, eliminating repetitive lookup code and streamlining your API controllers.

Why EntityModelBinder?

In RESTful APIs, you often need to fetch database entities based on route parameters. Instead of writing repetitive lookup code in every endpoint, EntityModelBinder automatically resolves the entity for you—exactly like Laravel's route-model binding.

Installation

dotnet add package EntityModelBinder

Quick Start

1. Implement IEntity on your database model

using EntityModelBinder;

public class Product : IEntity
{
    public int Id { get; set; }
    public string Slug { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

2. Register the service in your Program.cs

builder.Services.AddEntityModelBinder<SomePostgresContext>(options =>
{
    options.EnableSwaggerSchemaIds = true;
    options.SuppressModelStateInvalidFilter = true;
});

3. Use in your API controllers

[HttpGet("products/{product}")]
public IActionResult Get([FromRoute] Product product)
{
    if (product == null) return NotFound();
    return Ok(product);
}

That's it! The model is automatically fetched from the database. ✌️

Advanced Usage

Using Custom Column Keys with [EntityKey]

By default, the binder searches by Id. Use the [EntityKey] attribute to bind by any column:

[HttpGet("products/{product}")]
public IActionResult Get([FromRoute, EntityKey("Slug")] Product product)
{
    if (product == null) return NotFound();
    return Ok(product);
}

Supported types: int, Guid, string (slugs).

Real-World Example

Before (without EntityModelBinder):

[HttpGet("{surveyId:int}")]
public async Task<IActionResult> Show([FromRoute] int surveyId)
{
    var survey = await _dbContext.Survey.FindAsync(surveyId);
    return Ok(survey);
}

[HttpPatch("{surveyId:int}")]
public async Task<IActionResult> Update([FromRoute] int surveyId, [FromBody] SurveyRequest request)
{
    var survey = await _dbContext.Survey.FindAsync(surveyId);
    survey.Name = request.name;
    survey.Json = request.jsonData;
    survey.UpdatedAt = DateTime.UtcNow;
    await _dbContext.SaveChangesAsync();
    return Ok(new { id = survey.Id });
}

After (with EntityModelBinder):

[HttpGet("{survey}")]
public IActionResult Show([FromRoute] Survey survey)
{
    if (survey == null) return NotFound();
    return Ok(survey);
}

or in some cases

[HttpGet("{survey}")]
public IActionResult Show([FromRoute] Survey survey)
    => Ok(survey);

Adding a new resource

[HttpPost]
public async Task<IActionResult> Store([FromBody] SurveyRequest request)
{
    var entity = new Survey
    {
        Name = request.name,
        Json = request.jsonData,
        StructureId = _identityProvider.CurrentStructureId,
        CreatedAt = DateTime.UtcNow
    };
    _dbContext.Surveys.Add(entity);
    await _dbContext.SaveChangesAsync();
    return CreatedAtAction(nameof(Show), new { survey = entity.Id }, entity);
}

[HttpPatch("{survey}")]
public async Task<IActionResult> Update([FromRoute] Survey survey, [FromBody] SurveyRequest request)
{
    if (survey == null) return NotFound();

    survey.Name = request.name;
    survey.Json = request.jsonData;
    survey.UpdatedAt = DateTime.UtcNow;
    await _dbContext.SaveChangesAsync();
    return Ok(survey);
}

Configuration

Swagger Support

If you're using Swagger/OpenAPI and encounter schema conflicts, enable the option:

builder.Services.AddEntityModelBinder(options =>
{
    options.EnableSwaggerSchemaIds = true;
});

Then in your Swagger configuration:

c.CustomSchemaIds(type => type.ToString());

How It Works

  1. When a request hits a route like /products/my-product-slug, the binder intercepts the parameter
  2. It checks for [EntityKey("ColumnName")] on the action parameter
  3. If found, uses that column; otherwise defaults to Id
  4. Determines the column type (int, Guid, or string)
  5. Queries the database using Entity Framework Core
  6. Returns the entity directly to your controller method
  7. Returns 404 Not Found if the entity doesn't exist

Requirements

  • .NET 8.0+
  • ASP.NET Core
  • Entity Framework Core 8.0+

License

MIT License - see LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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.  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 is compatible.  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
1.0.2 29 3/6/2026
1.0.1 36 3/5/2026
1.0.0 29 3/5/2026