DbModelGenerator 0.5.5

dotnet add package DbModelGenerator --version 0.5.5
                    
NuGet\Install-Package DbModelGenerator -Version 0.5.5
                    
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="DbModelGenerator" Version="0.5.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DbModelGenerator" Version="0.5.5" />
                    
Directory.Packages.props
<PackageReference Include="DbModelGenerator" />
                    
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 DbModelGenerator --version 0.5.5
                    
#r "nuget: DbModelGenerator, 0.5.5"
                    
#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 DbModelGenerator@0.5.5
                    
#: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=DbModelGenerator&version=0.5.5
                    
Install as a Cake Addin
#tool nuget:?package=DbModelGenerator&version=0.5.5
                    
Install as a Cake Tool

DbModelGenerator

A .NET Roslyn incremental source generator that generates POCO record classes from SQL migration scripts.

It is compatible with PostgreSQL syntax and pairs well with Dapper, DapperExtensions, and DbUp for a true database-first workflow.

How it works

  1. You write SQL migration scripts (CREATE TABLE, ALTER TABLE, …) organised in sub-directories under Scripts/.
  2. DbModelGenerator reads those scripts at compile time (as a Roslyn analyzer) and generates one C# record per table.
  3. Generated classes are injected directly into your compilation — no files to commit, no pre-build step to configure.

Installation

Add the package to your .csproj:

<ItemGroup>
    <PackageReference Include="DbModelGenerator" Version="0.5.2"/>
</ItemGroup>

Then expose your SQL migration scripts as AdditionalFiles so the analyzer can read them:

<ItemGroup>
    <AdditionalFiles Include="Scripts\**\*"/>
</ItemGroup>

That's all. The generator runs automatically on every build.

Scripts directory structure

MyProject/
└── Scripts/
    ├── db.json          ← optional configuration file
    ├── Global/          ← one namespace per sub-directory
    │   └── 01_create_tables.sql
    └── Tenant/
        ├── 01_create_tables.sql
        └── 02_alter_table.sql
  • Each sub-directory maps to a generated C# namespace: {ProjectName}.Generated.Db.{SubDirectoryName}.
  • Scripts inside a sub-directory are applied in alphabetical order (standard DbUp convention).
  • The generator supports CREATE TABLE, ALTER TABLE (add/drop/rename column, add/drop constraint), and DROP TABLE.

Example

Given Scripts/Tenant/01_create_tables.sql:

CREATE TABLE user_profile
(
    id        SERIAL       NOT NULL,
    email     VARCHAR(255) NOT NULL,
    firstName VARCHAR(255) NOT NULL,
    disabled  BOOLEAN      NOT NULL DEFAULT '0',
    PRIMARY KEY (id)
);

The generator produces (in MyProject.Generated.Db.Tenant):

public sealed record UserProfile(
    int Id,
    string Email,
    string FirstName,
    bool Disabled
);

Configuration — Scripts/db.json

Place an optional db.json file directly inside the Scripts/ directory to customise code generation:

{
  "interfaces": [
    "My.Namespace.IEntity",
    "My.Namespace.IDbEntity(created_by)"
  ],
  "primaryKeyAttribute": "My.Namespace.PrimaryKey",
  "autoIncrementAttribute": "My.Namespace.AutoIncrement",
  "suffix": "Db",
  "ignores": ["audit_log", "migration_history"]
}
Field Description
interfaces List of C# interfaces that matching classes should implement (see below).
primaryKeyAttribute Fully-qualified attribute applied to primary key properties.
autoIncrementAttribute Fully-qualified attribute applied to auto-increment (SERIAL) properties.
suffix Text appended to every generated class name (e.g. "Db"UserProfileDb).
ignores Table names to exclude from generation (case-insensitive).

Interface matching

An interface is applied to a class only when all the listed properties exist on the table.

"interfaces": [
    "My.App.IEntity",
    "My.App.IDbEntity(created_by)"
]
  • My.App.IEntity — applied to every table that has an id column (default property when none specified).
  • My.App.IDbEntity(created_by) — applied only to tables that have a created_by column.

Generic type parameters — suffix a property name with ! to use its C# type as a generic type argument:

"My.App.IEntity(id!)"

This generates IEntity<int> when id is SERIAL, IEntity<string> when id is TEXT, etc.

Custom scripts directory

By default the generator looks for scripts in {ProjectDir}/Scripts. Override this with the ScriptsDir MSBuild property:

<PropertyGroup>
    <ScriptsDir>Migrations</ScriptsDir>
</PropertyGroup>

SQL type mapping (PostgreSQL)

SQL type C# type
SERIAL / INTEGER / INT int
SMALLINT / SMALLSERIAL short
BIGINT / BIGSERIAL long
TINYINT byte
BOOLEAN / BIT bool
REAL / NUMERIC / DECIMAL / DOUBLE PRECISION / MONEY decimal
TEXT / VARCHAR / CHAR (and any unrecognised type) string
BINARY / VARBINARY / BLOB byte[]
DATE DateOnly
TIME TimeOnly
TIMESTAMP / DATETIME DateTime
UUID / UNIQUEIDENTIFIER Guid

Nullable columns (without NOT NULL) produce nullable C# types (int?, string?, …). SERIAL / SMALLSERIAL / BIGSERIAL columns are always nullable (int?) since their value is assigned by the database on insert.

Complete example

See the Example.Service project for a working end-to-end setup.

There are no supported framework assets in this 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
0.5.5 0 3/10/2026
0.5.4 0 3/10/2026
0.5.3 0 3/10/2026
0.5.2 147 2/26/2026
0.5.1 82 2/25/2026
0.5.0 86 2/25/2026
0.4.7 9,213 2/15/2021
0.4.6 528 2/3/2021
0.4.5 595 1/18/2021
0.4.4 516 1/18/2021
0.4.3 1,053 10/19/2020
0.4.2 881 9/2/2020
0.4.1 629 9/2/2020
0.4.0 622 9/2/2020
0.3.9 595 9/2/2020
0.3.8 591 9/2/2020
0.3.7 660 9/1/2020
0.3.6 630 8/31/2020
0.3.5 617 8/31/2020
0.3.4 648 8/31/2020
Loading failed