MH.DbToEntity.CLI
1.2.0
dotnet tool install --global MH.DbToEntity.CLI --version 1.2.0
dotnet new tool-manifest
dotnet tool install --local MH.DbToEntity.CLI --version 1.2.0
#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
updatecommand allows you to regenerate specific entities non-destructively. - Project Awareness: Automatically detects your connection string from
appsettings.jsonand namespace from your.csprojfile. - Fluent API: Generates complete
OnModelCreatingconfigurations (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:
- Read
ConnectionStrings:DefaultConnection(orPostgres) from yourappsettings.json. - Detect your project's namespace from the
.csprojfile in the current directory. - Generate entities in
./Entitiesand yourDbContext.
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 toappsettings.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
generateapply.
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→ ClassUser - Column
first_name→ PropertyFirstName - Foreign Key
role_id→ NavigationRole
Uninstalling
dotnet tool uninstall -g DbToEntity.CLI
| Product | Versions 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. |
This package has no dependencies.