A CLI tool to generate EF Core entities from PostgreSQL, supporting partitioned tables and incremental updates.
$ dotnet add package MH.DbToEntity.CLIA 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:
update command allows you to regenerate specific entities non-destructively.appsettings.json and namespace from your .csproj file.OnModelCreating configurations (Keys, Constraints, Relationships, Defaults) rather than just attributes.Install explicitly from NuGet.org:
dotnet tool install --global MH.DbToEntity.CLI
To get the latest version later:
dotnet tool update --global MH.DbToEntity.CLI
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.
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:
ConnectionStrings:DefaultConnection (or Postgres) from your appsettings.json..csproj file in the current directory../Entities and your DbContext.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).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.generate apply.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);
});
}
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.
Uses Humanizer to ensure C# conventions:
users -> Class Userfirst_name -> Property FirstNamerole_id -> Navigation Roledotnet tool uninstall -g DbToEntity.CLI