Skip to content

itsmanwar/DbToEntity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Generate clean .NET entities and DbContext from your database schema

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages