Generate NHibernate Classes and Mapping with Ease
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.
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.
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.
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.
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 the sample name.
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.
In the generation settings you may perform additional tweaking of the generation of NHibernate entities and mapping. Entity Developer allows you to generate classes with or without Equals and GetHashCode implementation, IValidatable interface implementation, and support for IPropertyChanging and IPropertyChanged events. It can generate partial classes with one file for your code and one file for the generated code (Code-behind) to avoid overwriting user changes for the generated class. For more details see the NHibernate entity generator article.
As for generating NHibernate mapping, Entity Developer can generate both XML and fluent 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.
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 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.
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:
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.
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.
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.