How to Create and Configure an NHibernate Model

This tutorial introduces you to Entity Developer and guides you through configuring and building an NHibernate model. You'll also discover how its visual design tools and easy database integration streamline the development process.

Prerequisites

Here is a list of applications and tools used in this tutorial to demonstrate how to configure and create an NHibernate model:

  • Visual Studio 2022: The IDE used in the tutorial. If you do not have it installed, visit the official Microsoft website to download the Community edition, which is free and available to everyone.
  • dotConnect or any open-source package: In this tutorial, we will use the open-source MySql.Data core package.
  • Entity Developer: A standalone tool used to generate NHibernate models.
  • Sakila database: A sample database used for testing purposes. To use the same database, simply download it and unzip the folder.
  • NHibernate: A mature, open-source object-relational mapper for .NET frameworks.

Why Entity Developer?

Entity Developer is a powerful ORM designer for ADO.NET Entity Framework, EF Core, NHibernate, LinqConnect, and Telerik Data Access. It allows developers to design data models visually, generate code automatically, and streamline database application development.

Entity Developer works with both Database-First and Model-First approaches, offering a visual interface to design models without writing code.

The Database-First approach is ideal when working with an existing database, allowing developers to reverse-engineer a model and quickly generate code from the database schema. This method is very popular among developers who prioritize database design and want to work with an existing database structure.

Meanwhile, the essential concept of the Model-First approach is defining a data model first and then generating both the database schema and the corresponding entity classes from that model. This approach is beneficial if you want to focus on designing your data model first and have NHibernate automatically generate the necessary code and database schema for you.

In this tutorial, you'll learn how to create an NHibernate model using both methods.

A standalone application vs. a Visual Studio-integrated version

Entity Developer is available both as a Visual Studio add-in and as a standalone application, giving you flexibility depending on your development preferences.

In this tutorial, we will show you how to configure and create an NHibernate model using the standalone version.

Standalone vs. VS-integrated application

The Database-First approach

In this section, you will learn how to create and update models using the Database-First approach in Entity Developer, our standalone modeling and code generation tool.

Create a NHibernate model

1. Launch Entity Developer.

2. On the toolbar, select File > New Model.

Add new model

3. In the New Model dialog that appears, choose NHibernate Model and click Create.

Select NHibernate Model

4. In the Create Model Wizard, select Database First and click Next.

Select Database First

5. Next, select a data provider. In this case, choose the MySql.Data open-source connector. Then, enter the connection details and click Test Connection to verify the connection is successful. Once confirmed, click Next.

Select MySql.Data open-source connector

6. Specify whether you want to create your model based on an existing database or start from scratch. In this tutorial, we will generate it from a database. Click Next to continue.

Choose model content

7. Choose the database objects you want to scaffold. Then, click Next.

Select database objects

8. Define the naming convention for the property names in the database object and click Next. We suggest keeping the default settings this time.

Define naming convention

9. On the Model properties page, configure your model settings. Once completed, click Next.

Useful tips:
  • Keep the Detect Many-to-Many associations option selected to automatically detect many-to-many associations.
  • Keep the Detect Table Per Type inheritances option selected to automatically detect inheritances of that type.
Configure the model settings

10. Choose the model diagram content. You can use all entities, split the entities by database, or do a custom selection. For this tutorial, select All Entities and click Next.

Choose the model diagram content

11. Choose code generation templates for your model. You can define different parameters you want the model to follow. Let's use the default settings for this tutorial. Click Next.

Choose code generation templates for your model

Your model is ready now. Finally, click Finish to close the wizard.

The model is ready

Once the model is created, it opens in Entity Developer.

Model opens in Entity Developer

Before working with the model, consider the following steps:

1. Save the model to the project directory.

Save the model

2. Next, generate code and dbcontext classes. For this, select Model > Generate Code.

Generate code

You will see the code generation in progress.

Code generation in progress

When the code generation is complete, review the generated model classes. Note that Entity Developer generates both C# and XML code to ensure compatibility with the NHibernate model.

Code generation completed

Update the model diagram

Updating the model diagram involves modifying classes, properties, their relationships, and other related elements. In this tutorial, we will focus on how to update model properties.

1. Right-click the created model and select Add > New Property.

2. In the Property Editor dialog, define property settings and click OK to save your changes.

  • Name: Genre
  • Type: String
  • Column: Genre
Define property settings

Update the database from the model

1. On the toolbar of Entity Developer, select Model > Update Database From Model.

Update database from model

2. In the Update To Database Wizard, ensure the Recreate Database Table and Regenerate Storage options are unchecked and click Next.

In addition to updating the database, the wizard also allows you to completely regenerate it or update only the storage part of the model. Simply select the appropriate option on the wizard page and follow the on-screen instructions.
Welcome to Entity Developer Update To Database Wizard

3. Select the database schema(s) that you want to update. In our case, we are adding the sakila database. Click Next to proceed.

Select database schema(s)

4. Review the list of database objects that will be scripted and uncheck those you do not want to include. Click Next to continue.

Select tables to script

5. Review the action plan to make sure it meets your requirements. Click Execute to run the script.

Review script

6. Once the script executes successfully, click Finish to close the wizard.

Script is successfully executed

7. Save the model to the project directory and verify that the new property appears in the generated code.

Save and verify model

Update the model from the database

Let's suppose we have added the Language column to the film_category table. After this change in the database, we now need to update the model.

1. On the toolbar of Entity Developer, select Model > Update Model From Database.

Update database from model

2. In the Update From Database Wizard, ensure the Create Model option is unchecked and click Next.

Welcome to Entity Developer Update To Database Wizard

3. Select the database schema(s) where you have added changes. In our case, we're selecting the sakila database. Click Next to proceed.

Select database schema(s)

4. Select the database objects that have been changed, then click Next.

Select tables to script

5. After the update is complete, click Finish to close the wizard.

Script is successfully executed

Finally, you can see the updated model in Entity Developer.

Save and verify model

The Model-First approach

This section will guide you through creating and updating models using the Model-First approach in Entity Developer, our dedicated tool for modeling and code generation.

Create an NHibernate model

1. Launch Entity Developer.

2. On the toolbar, select File > New Model.

Add new model

3. In the New Model dialog that appears, choose NHibernate Model and click Create.

Select NHibernate Model

4. In the Create Model Wizard, select Model First and click Next.

Select Model First

5. On the Model properties page, configure your model settings. Once completed, click Next.

Useful tips:
  • Keep the Detect Many-to-Many associations option selected to automatically detect many-to-many associations.
  • Keep the Detect Table Per Type inheritances option selected to automatically detect inheritances of that type.
Configure model settings

6. Choose code generation templates for your model. You can also define specific parameters to customize how the model is generated. Let's stick to the default settings for this tutorial and click Next.

Choose a template on the Code Generation Templates page of the Create Model Wizard

The model is ready now. Click Finish to close the wizard.

Choose code generation templates

Add classes and properties

1. Ensure the DataModel is open.

2. Right-click your model and select Add > New Class.

Add class

3. In the Class Editor dialog, type a class name in the Name field and click OK. For this tutorial, use Customers as the class name.

Define class name

The created class then opens in the model.


After creating a class, you can define its properties.

1. Right-click the class and select Add > New property.

Add property

2. In the Property Editor dialog, configure the class properties as needed, and click OK to save the property settings. In our example, the properties are defined as follows:

  • Name: Id
  • Type: Int32
  • Primary key: Select the checkbox to indicate that this class includes a primary key
  • Column: Id
Property Editor

Add associations

Assume you've created two classes and want to define an association between them. In our example, we'll create an association between the Customers and Orders classes, where the CustomerId in the Order class will be a foreign key referencing the Id column in the Customers class.

To create the association:

1. Right-click Customers in the model and select Add > New Association from the menu.

Add association

2. In the Association Editor dialog that appears, configure the required properties and click OK. Configure association properties

Once added, the association will be displayed in the model diagram.

Established association

Add inheritances

You can extend the current model by adding the third PremiumCustomer class, which will be configured as a derived class from the previously created Customers class using the Id column as a base identifier. For this, follow the steps below:

1. Create the PremiumCustomer class and configure its properties.

2. Right-click the model and select Add > New Inheritance from the context menu.

New inheritances

3. In the Inheritance Editor dialog, configure an inheritance class and set the discriminator column, then click OK. Let's configure the following properties:

  • Base Class: Customers
  • Derived Class: PremiumCustomers
  • Type: Table Per Hierarchy
  • For Derived Class: Id
  • Column: CustomersDescriminator
  • Type: String
Configure inheritance class

As a result, PremiumCustomers becomes a subclass of Customers, adding inheritance to the model.

Added inheritance

4. Now, save the model. Once saved, generate the code by selecting Model > Generate Code on the toolbar. You will then see the code generation in progress.

Code generation completed

Generate an SQL database script

With Entity Developer, you can generate database scripts from any NHibernate model, whether created using the Database-First or Model-First approach.

1. Ensure the model is all set.

2. On the toolbar, select Model > Generate Database Script From Model.

Generate database script from model

3. In the Generate Database Script Wizard that appears, leave the Include Drop and Regenerate Storage checkboxes cleared and click Next to proceed.

Welcome to Entity Developer Generate Database Script Wizard

4. Configure the model synchronization settings and click Next.

  • Target Server: MySQL
  • Server Version: 8.4
  • Data Provider: MySql.Data.MySqlClient
 Configure model synchronization settings

5. Review the database objects that will be scripted and clear the checkboxes next to those you do not want to include. Click Next to continue.

Select tables to script

Now, review the script to ensure it is successfully generated. Click Finish to close the wizard.

Generated script

Benefits of using Entity Developer

Entity Developer is an enterprise-grade tool that makes ORM design faster, more efficient, and error-free. It's an indispensable, intuitive solution for developers of all skill levels — and here's why:

  • Accelerated development workflow: Keep your model and database in sync with automatic update and code regeneration features.
  • Comprehensive ORM support: Work with EF Core, Entity Framework, LinqConnect, and NHibernate — all within a single tool.
  • Seamless Visual Studio integration: Operate within Visual Studio for a smooth development experience.
  • Broad compatibility: Cover all major relational databases like SQL Server, Oracle, MySQL, PostgreSQL, and SQLite.

Video tutorial

Conclusion

This tutorial gives detailed instructions on how to create and update models using the Database-First and Model-First methods in Devart Entity Developer. The implementation of the Model-First method means that the developer defines the Model-First, and EF generates a database using this model. Meanwhile, in the Database-First approach, the model is generated using an existing database. Both approaches are thoroughly explained in this tutorial, with detailed code snippets, illustrations, screenshots, and additional resources.