Generate NHibernate Classes and Mapping with Ease

NHibernate is a widely used ORM solution for Microsoft .NET - it helps mapping .NET classes to database tables and creating a data access layer with little effort. NHibernate Generator approach supposes generating NHibernate entity code and NHibernate mapping based on an existing database schema. This approach is rather popular and there is a number of tools implementing it.

NHibernate Generation Process

Entity Developer for NHibernate offers advanced NHibernate generator functionality for generating NHibernate entity code and NHibernate mapping. To generate entity code and mapping from a database, you need to perform several simple steps, described below.


1. Connecting to a Database

To connect to a database you need to specify the provider and the connection settings. Entity Developer for NHibernate supports any third-party ADO.NET data providers for SQL Server, Oracle, MySQL, PostgreSQL, and SQLite. When using Entity Developer integrated to Visual Studio, Entity Developer supports provider-specific connection dialogs to specify the connection settings.

Specifying database connection settings

2. Selecting Objects

Entity Developer allows you to select tables and views for generating entities. Tables and views are grouped into the corresponding nodes. You may choose whether to view objects from all the schemas or only from the current one, specified in the connection. In case you are selecting objects from multiple schemas, you may ease the selecting of the database objects by enabling grouping objects by schemas.

Selecting database objects

3. Setting Naming Rules

Entity Developer allows flexible tweaking naming rules for the generated names of the classes and their members. Entity Developer allows you to configure case and pluralization, removing and adding of prefixes and suffixes, add the schema name as the prefix, remove underscores from the database object names, etc. You may immediately view how the names are changed on a sample name.

Naming rules

4. Tweaking Model Settings

On this step you can set the default parameters for the generated classes: default assembly and namespace, default schema, cascade style, strategy for accessing a property value, default identity generator and its parameters, etc. You can also enable or disable automatic detection of many-to-many associations and TPT inheritances.

Entity Developer allows you to generate mapping containing all the schema details or preserve only the cross-database schema information to create database-independent models. Alternatively you may disable storing the database schema information in the mapping.

Model settings

5.Customizing Generation

Entities Generation

In the generation settings you may perform additional tweaking of the generation of NHibernate entities and mapping. Entity Developer offers a number of settings to configure different aspects of entity generation:

  • Well-known Interface Implementation

    Generate classes with or without Equals and GetHashCode implementation, IValidatable interface implementation, and support for IPropertyChanging and IPropertyChanged events.

  • WCF Support

    Set the Generate Data Contract Attribute property for entity classes and the Generate Data Member Attribute property for entity properties - for all model entities and properties together or for each one separately.

  • Custom .NET Attribute Support

    Easily assign attributes from available assemblies to classes, properties or contexts of a model.

Setting template properties

Mapping Generation

As for generating NHibernate mapping, Entity Developer can generate XML, Fluent NHibernate, and Loquacious (Mapping by code) mapping.

For XML mapping you may customize output settings, use the File per class option. XML mapping is generated as an embedded resource by default, however you may change this behaviour and generate mapping as separate XML files.

Entity Developer provides full support for Fluent NHibernate mapping - it supports all kinds of inheritances, components, composite IDs, etc.

Configuration Generation

Entity Developer for NHibernate is also capable to generate NHibernate configuration XML code. You may tweak the NHibernate configuration settings in the Model Settings dialog box and then generate the configuration code either to the App.config file of the project or to a separate *.cfg.xml file.

Why NHibernate Generator Is Not Enough

When using this NHibernate generator approach, often you still need to perform model editing. Such mapping as components, TPH and TPC hierarchies, etc. cannot be detected automatically. Entity Developer is more than just an NHibernate generator, it is a powerful NHibernate designer.

With our NHibernate designer developing NHibernate models becomes easy and intuitive. It allows you to perform additional tweaking of NHibernate model visually in convenient editor dialog boxes without editing *.hbm.xml mapping files manually. Such common NHibernate mapping cases as components and TPH inheritance can be created in a few clicks from a model, generated from a database. You need just to drag several properties from the class to create a base or derived class or a component from them.

Get Even More with Code Generation T4 Templates

Template Code Generation System

If you need a more flexible customization of the code generation, you can get more familiar with the Entity Developer template code generation system.

Entity Developer has a very flexible code generation system, based on templates, written in T4-like template language. Entity Developer comes with predefined templates for generating NHibernate C# and Visual Basic code, however you can create your own templates for specific use cases to tweak code generation. Templates are created with a handy editor, which provides context-sensitive code completion, error check, syntax highlighting and other features to speed up your work.

Template editor

Flexible Output Configuration

With Entity Developer templates you may configure code output in a more flexible way. Output settings are configured with the PushOutputRedirection function that can be called inside the template code.

This function is used to configure the following aspects of code generation:

  • Overwrite behavior
    The OverwriteMode parameter can be set to one of the following values:

    • None - generated files are not overwritten when the model is saved;
    • Merge - the new version of generated files is appended to the previous one when the project is saved;
    • Overwrite - generated files are overwritten when the project is saved.
  • Code-behind
    The ParentFileName parameter allows you to specify the parent node in the Solution Explorer.
  • Output destination
    The OutputInfo structure allows you to specify the output folder (applied in the stand-alone Entity Developer application) or the output project and project folder (applied in the Visual Studio add-in).
  • Visual Studio project item properties
    CopyToOutputDirectory and BuildAction parameters specify the corresponding Visual Studio project item properties.

Additional Properties

To additionally tweak code generation, you may define additional properties for the model and its objects. These properties may be used by the Entity Developer in the template code. For example, you may add a Generate property for classes, set it when designing the model, and, depending on the property value, our NHibernate generator will generate code for the classes or skip them.

Additional properties for model objects