nh-tool
1.2.0
dotnet tool install --global nh-tool --version 1.2.0
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest
dotnet tool install --local nh-tool --version 1.2.0
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=nh-tool&version=1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
nuke :add-package nh-tool --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
nh-tool
CLI tool that scaffolds NHibernate Mapping-by-Code entities and mappings from your database schema. Think dotnet ef dbcontext scaffold, but for NHibernate 5+.
Install
dotnet tool install --global nh-tool
Usage
nh-tool nh-scaffold <connection-string> <provider> [options]
Quick Examples
# SQL Server - all tables
nh-tool nh-scaffold "Server=.;Database=MyDb;Trusted_Connection=true;TrustServerCertificate=true" sqlserver \
-o ./Generated -n MyApp.Data
# Oracle - specific tables only
nh-tool nh-scaffold "Data Source=myhost:1521/ORCL;User Id=HR;Password=hr123;" oracle \
-t EMPLOYEES,DEPARTMENTS -o ./Generated -n HrModule.Data -s HR
# Exclude tables + force overwrite
nh-tool nh-scaffold "Server=.;Database=MyDb;..." sqlserver \
-x AUDIT_LOG,TEMP_DATA --force -o ./Generated -n MyApp.Data
# Preview without writing files
nh-tool nh-scaffold "Server=.;Database=MyDb;..." sqlserver --dry-run
Options
| Option | Alias | Default | Description |
|---|---|---|---|
--output |
-o |
./Generated |
Output directory |
--namespace |
-n |
MyApp.Data |
Root namespace |
--schema |
-s |
dbo / connected user |
Schema or owner filter |
--tables |
-t |
(all) | Comma-separated table include list |
--exclude-tables |
-x |
(none) | Comma-separated table exclude list |
--use-legacy-style |
-l |
false |
.NET Framework compatible output (C# 7.3) |
--dry-run |
-d |
false |
Preview files without writing |
--force |
-f |
false |
Overwrite existing files |
What Gets Generated
Generated/
Entities/
User.cs # POCO with virtual properties + navigation
Order.cs
OrderItem.cs
Mappings/
UserMap.cs # ClassMapping<T> with ManyToOne/Bag
OrderMap.cs
OrderItemMap.cs
NHibernateHelper.cs # Thread-safe ISessionFactory configuration
Entity with Navigation Properties
public class Order
{
public virtual int Id { get; set; }
public virtual DateTime OrderDate { get; set; }
public virtual decimal TotalAmount { get; set; }
public virtual Customer Customer { get; set; } // ManyToOne
public virtual IList<OrderItem> OrderItems { get; set; } = new List<OrderItem>(); // OneToMany
}
Mapping with FK + Identity Support
public class OrderMap : ClassMapping<Order>
{
public OrderMap()
{
Schema("dbo");
Table("ORDERS");
Id(x => x.Id, m =>
{
m.Column("ID");
m.Generator(Generators.Identity); // Auto-detected from SQL Server IDENTITY
});
Property(x => x.OrderDate, m =>
{
m.Column("ORDER_DATE");
m.NotNullable(true);
});
ManyToOne(x => x.Customer, m =>
{
m.Column("CUSTOMER_ID");
m.NotNullable(true);
});
Bag(x => x.OrderItems, c =>
{
c.Key(k => k.Column("ORDER_ID"));
c.Inverse(true);
c.Lazy(CollectionLazy.Lazy);
}, r => r.OneToMany());
}
}
SessionFactory Helper
var factory = NHibernateHelper.BuildSessionFactory("Server=.;Database=MyDb;...");
using var session = factory.OpenSession();
// Optional cleanup APIs:
NHibernateHelper.DisposeSessionFactory("Server=.;Database=MyDb;...");
NHibernateHelper.DisposeAllSessionFactories();
Supported Databases
| Provider | CLI name | Driver | Dialect |
|---|---|---|---|
| SQL Server 2012+ | sqlserver or mssql |
MicrosoftDataSqlClientDriver |
MsSql2012Dialect |
| Oracle 12c+ | oracle |
OracleManagedDataClientDriver |
Oracle12cDialect |
Key Features
- Foreign Keys - Auto-detected ManyToOne + OneToMany navigation properties
- Composite FK handling - Keeps scalar FK columns and warns that composite navigations require manual mapping
- Shared-PK safety - Skips auto ManyToOne when FK column is part of PK to avoid repeated column mapping
- IDENTITY / Sequence - SQL Server IDENTITY and Oracle sequences auto-detected
- Mapping-by-Code nativo - No FluentNHibernate, uses NHibernate 5+
ClassMapping<T> - Smart naming -
USERS→User,CUSTOMER_ID→Customernavigation (Humanizer) - Composite PKs - Detected and mapped with
ComposedId - PK strategy -
Identity,Sequence,Native, orAssignedbased on column metadata - Thread-safe session factory cache - Generated
NHibernateHelperusesConcurrentDictionary<string, Lazy<ISessionFactory>> - Table filtering -
--tablesto include,--exclude-tablesto exclude - Dry run - Preview generated files and composite-FK warnings before writing
- Force overwrite -
--forceto overwrite existing files - Legacy mode -
--use-legacy-stylefor .NET Framework / C# 7.3 compatibility
Requirements
- .NET 8.0+
License
MIT
| 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.