DbModelGenerator 0.5.5
dotnet add package DbModelGenerator --version 0.5.5
NuGet\Install-Package DbModelGenerator -Version 0.5.5
<PackageReference Include="DbModelGenerator" Version="0.5.5" />
<PackageVersion Include="DbModelGenerator" Version="0.5.5" />
<PackageReference Include="DbModelGenerator" />
paket add DbModelGenerator --version 0.5.5
#r "nuget: DbModelGenerator, 0.5.5"
#:package DbModelGenerator@0.5.5
#addin nuget:?package=DbModelGenerator&version=0.5.5
#tool nuget:?package=DbModelGenerator&version=0.5.5
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
- You write SQL migration scripts (CREATE TABLE, ALTER TABLE, …) organised in sub-directories under
Scripts/. - DbModelGenerator reads those scripts at compile time (as a Roslyn analyzer) and generates one C#
recordper table. - 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), andDROP 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 anidcolumn (default property when none specified).My.App.IDbEntity(created_by)— applied only to tables that have acreated_bycolumn.
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.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.Build.Utilities.Core (>= 18.3.3)
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
- System.Collections.Immutable (>= 10.0.3)
- System.Text.Json (>= 10.0.3)
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 |