MH.DbToEntity.CLI 1.2.0

dotnet tool install --global MH.DbToEntity.CLI --version 1.2.0
                    
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
                    
if you are setting up this repo
dotnet tool install --local MH.DbToEntity.CLI --version 1.2.0
                    
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=MH.DbToEntity.CLI&version=1.2.0
                    
nuke :add-package MH.DbToEntity.CLI --version 1.2.0
                    

DbToEntity

A Smart EF Core Entity Generator for PostgreSQL

DbToEntity is a CLI tool designed to generate Entity Framework Core entities from an existing PostgreSQL database. It distinguishes itself with:

  • Partitioning Awareness: Automatically detects partitioned tables and generates entities only for the parent table.
  • Incremental Updates: The update command allows you to regenerate specific entities non-destructively.
  • Project Awareness: Automatically detects your connection string from appsettings.json and namespace from your .csproj file.
  • Fluent API: Generates complete OnModelCreating configurations (Keys, Constraints, Relationships, Defaults) rather than just attributes.


Installation

1. Install Global Tool (Standard)

Install explicitly from NuGet.org:

dotnet tool install --global MH.DbToEntity.CLI

2. Update Tool

To get the latest version later:

dotnet tool update --global MH.DbToEntity.CLI

Troubleshooting

Error: "Package type 'DotnetTool' is not supported"

If you see this, you likely tried to "Manage NuGet Packages" and add it to a project. Do not do that. This is a CLI tool. Install it on your machine using the console commands above. Then run it using db2entity generate.

Quick Start

If you are running this inside a .NET project that already has an appsettings.json with a connection string:

db2entity generate

That's it! The tool will:

  1. Read ConnectionStrings:DefaultConnection (or Postgres) from your appsettings.json.
  2. Detect your project's namespace from the .csproj file in the current directory.
  3. Generate entities in ./Entities and your DbContext.

Detailed Usage

1. Generate Entities (Initial Scaffold)

If you need to override defaults or run outside a project:

db2entity generate --connection "Host=localhost;Database=mydb;..." --schema "public" --output "./Data/Entities" --namespace "MyApp.Domain"

Options:

  • --connection: (Optional) PostgreSQL connection string. Defaults to appsettings.json.
  • --schema: (Optional) Schema to target (default: all schemas).
  • --output: (Optional) Output directory (default: ./Entities).
  • --namespace: (Optional) Namespace (default: detected from .csproj).
  • --separate-by-schema: (Optional) Organize into subfolders by schema (default: false).
  • --tables: (Optional) Generate only specific tables (space separated).

2. Update Specific Tables (Incremental)

When you modify a specific table (e.g., adding a column to users), use update to refresh just that entity and the DbContext without checking out the whole database again.

db2entity update --tables "users"

Options:

  • --tables: (Required) List of tables to update.
  • All other options from generate apply.

Features

Fluent API Generation

Instead of cluttering your classes with attributes, DbToEntity generates a clean OnModelCreating method:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>(entity =>
    {
        entity.ToTable("users", "public");
        entity.HasKey(e => e.Id).HasName("users_pkey");
        entity.Property(e => e.Email).HasColumnName("email").IsRequired().HasMaxLength(255);
        entity.HasOne(d => d.Role).WithMany().HasForeignKey(d => d.RoleId);
    });
}

Schema-based Folder Organization (v1.1.7+)

Use --separate-by-schema to organize entities into subfolders based on their database schema:

  • public.users./Entities/User.cs (Namespace: MyApp.Domain)
  • logs.audit_trail./Entities/Logs/AuditTrail.cs (Namespace: MyApp.Domain.Logs)

This keeps your project clean when dealing with large databases.

Intelligent Naming

Uses Humanizer to ensure C# conventions:

  • Table users → Class User
  • Column first_name → Property FirstName
  • Foreign Key role_id → Navigation Role

Uninstalling

dotnet tool uninstall -g DbToEntity.CLI
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 was computed.  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 was computed.  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.

This package has no dependencies.

Version Downloads Last Updated
1.2.0 105 12/31/2025
1.1.7 97 12/27/2025
1.0.7 93 12/27/2025
1.0.6 93 12/27/2025