Creating Model Using Model-First Approach

ExpandedToggleIcon        Creating a Model Using Model-First Approach

This section explains how to create a model using the Model-First approach.

To create a model using the Model-First approach, perform the following sequence of actions:

1.In the Solution Explorer window, right-click the name of your project.
2.From the shortcut menu, select the Add command and select the New Item option.

note Note

In the standalone application click the Create New Model button on the Entity Developer toolbar.

Images_AddNewItemMF

3.In the Installed Templates column of the Add New Item window, select Data.
4.In the central column of the Add New Item window, select Devart NHibernate Model.

note Note

In the standalone application in the displayed New Model dialog select Devart NHibernate Model and click Create.

5.Click Add.

Images_CreateNewModel1MF

6.On the Create Model Wizard welcome page, select Model First and click Next.

Images_CreateNewModel6NH

7.On the Model properties page, select identity from the Name drop-down list in the Default Identity Generator area, define the settings of your model and click Next.

Images_CreateNewModel8

8.The Choose Code Generation page allows you to select code generation templates you want to add to the new model. By default this page contains the most frequently used template; the properties area of this page allows you to configure the properties of the selected template. Three buttons at the top of this page allow you to add more templates from gallery, add existing templates from disk and remove templates from the list.
9.Click the Add template button.

Images_CreateNewModel9

10. The Add Template dialog box lists all predefined and shared templates. Choose the required one and click OK. The selected template will be added to the list on the Choose Code Generation page together with all related templates (without which the template would work incorrectly).
11. After all the required templates are added to the list on the Choose Code Generation page of the wizard, click Next.
12. On the final window of the wizard click Finish.

After the empty diagram space is displayed, you can proceed to creating required classes, associations and inheritances.

The description of procedures for creating classes, associations and inheritances for the model is provided below.

Creating a Class

This procedure explains how to create a class. For the purpose of this example, we shall create the Blog class (see the picture below).

note Note

Before proceeding to creating classes, check that in the properties of your model identity is selected as the default generator. For this purpose, in the Model Explorer window, right-click your model, select Properties from the shortcut menu, and, in the Properties window, check that Default Identity Generator is set to identity.

1. Right-click on the empty diagram space and select New Class from the Add submenu of the shortcut menu.

Images_NewClass

2.Click the default name of the class (Class1), enter Blog and press ENTER.
3.Right-click the created class and select New Property from the Add submenu of the shortcut menu.

Images_PropertyEditorNHMF

4.In the Name box, enter the name of the property (BlogID).
5.From the Type drop-down list, select Int64.
6.Select the Primary Key check box.
7.(Optional) To change the default parameters of the database column, to which this property is to be mapped, click the three-dotted button in the Column field and, in the Column Editor window (see below), enter the desired values of the parameters and click OK.

Images_ColumnEditorNHMF

1.Click OK.
2.To create the second property (BlogTitle), right-click the created class and select New Property from the Add submenu of the shortcut menu.
3.In the Name box, enter the name of the property (BlogTitle).
4.From the Type drop-down list, select String.
5.(Optional) To change the default parameters of the database column, to which this property is to be mapped, click the three-dotted button in the Column field and, in the Column Editor window (see above), enter the desired values of the parameters and click OK.
6.Leave the other settings as they are and click OK.

All the other classes in the model are created similarly. For more information on how to create classes, see Classes.

Creating an Association

This procedure describes how to create an association. For the purpose of this procedure, we suppose that at least the Blog and Post classes have been created (see the picture of the example model in the Creating a Model Using Model-First Approach section). The association that is created in this procedure is of the One-to-Many type.

To create an association, perform the following sequence of actions:

1.On the model diagram, right-click the Blog class and, from the Add submenu of the shortcut menu, select New Association.

Images_AssociationEditorNHMF

2.In the End 1 area, set Class to Blog, Multiplicity to 1 (One) and leave other settings set to their default values.
3.In the End 2 area, set Class to Post, Multiplicity to * (Many) and leave other settings set to their default values.
4.(Optional) To create an association using a unique property instead of the primary key, under Foreign key references, select Unique property of Blog and, from the drop-down list, select the required unique property.
5.(Optional) To change the default parameters of the database column, in the Columns mapping area, click the three-dotted button in the Column field and, in the Column Editor window, enter the desired values of the parameters and click OK.
6.On the Association editor page, click OK.

For information on how create other types of associations, see Working with Associations.

Creating an Inheritance

This procedure describes how to create an inheritance. For the purpose of this procedure we suppose that the Author and RegularAuthor classes have already been created (see the picture of the example model in the Creating a Model Using Model-First Approach section). The inheritance that is created in this procedure is of the Table-per-Hierarchy type.

To create an inheritance, perform the following sequence of actions:

1.On the model diagram, right-click the Author class and, from the Add submenu of the shortcut menu, select New Inheritance.

Images_InheritanceEditorNHMF

 

2.From the Base Class drop-down list, select Author.
3.From the Derived Class drop-down list, select RegularAuthor.
4.From the Type drop-down list, select Table Per Hierarchy.
5.In the For Base Class box of the Discriminator Value area, specify the value for the base class (for example, "0")
6.In the For Derived Class box of the Discriminator Value area, specify the value for the derived class (for example, "1").
7.In the Discriminator area, select either Column or Formula and, from the Type drop-down list, select the appropriate type. If Column is selected, you can change the default settings of the column by clicking the three-dotted button in the Column box and specifying the required values on the Column editor page.
8.Leave the other settings set to their default values.
9.Click OK.

For more information on how to create inheritances of other types, see Working with Inheritances.

Summary Description of the Model

The resulting model should contain the following items:

Classes:
oBlog;
oBlogRoll;
oAuthor;
oRegularAuthor;
oGuestAuthor;
oPost;
oCategory.
Associations:
oBlog - Post, Blog - BlogRoll (one-to-many);
oBlog - Author (one-to-one);
oCategory - Post (many-to-many).
Inheritances:
oAuthor - Regular Author;
oAuthor - Guest Author.

Below is the example of the resulting model:

Images_SampleModelMFNH

 

ExpandedToggleIcon        Mapping and Code Generation Setup

This section explains how to set up mapping and code generation in NHibernate models.

For the purpose of mapping and code generation, you may need to create and define folders, into which mapping and generated code will be saved, as well as other parameters available in the Properties window of the selected template.

The definition of output folders for mapping and code generation is optional, if such folders are of no importance for you, go directly to Step 11 of the procedure.

To define folders for mapping and code generation, perform the following sequence of actions:

1.In your project in the Solution Explorer window, create a folder for mapping (for example, Mapping) and a folder for generated code (for example, Entities).
2.Navigate to the Model Explorer window, select the template and click to display the Properties window for the selected template.

Images_TemplatePropertiesNH

3.In the Output section of the model properties, click the Entities Output line and then click the displayed three-dotted button.
4.In the Select Project Folder dialog box, select the appropriate folder for entities (for example, the Entities folder that was created in Step 1 of this procedure) and click OK.
5.Select the appropriate value for the File Per Class property: if a value is set for the Entities Output parameter, the File Per Class parameter must be set to True.
6.To enable the entity property validation mechanism in the Validation section locate the Validation Framework row and select the appropriate framework; use the Validation Error Messages row to apply error messages stored in the application resources. For details on validation refer to Entity Property Validation.
7.In the Xml Mapping section of the template properties, select the appropriate value for the Xml Mapping Action property to define whether mapping will be copied to the folder specified in the Xml Mapping Output parameter, or to the folder storing the model file in case Xml Mapping Output parameter value is not specified, or it will be added to the project as an embedded resource.
8.To generate separate mapping files per class, set the Xml Mapping File Per Class property to True; to generate one mapping file, set this property to False.
9.Click in the Xml Mapping Output line and click the three-dotted line to open the Select Project Folder window.
10. In the Select Project Folder window, select the appropriate folder for entities (for example, the Mapping folder that was created in Step 1 of this procedure) and click OK.
11. (Optional) To specify additional parameters for your application (app.config) or web-application (web.config), for example, to change the Proxy Factory setting, right-click the diagram, select the Model Settings menu item from the shortcut menu and in the tree area select Configuration.

Images_MS_NH_2

note Note

On mapping and code generation, the NHibernate library is added to the configuration of your application, provided that this library is available from the Global Assembly Collection. If this library is stored elsewhere, in the Solution Explorer window, right-click Reference, select the Add reference command from the shortcut menu and follow the on-screen instructions to add the library to the configuration of your application.

12. To generate code and mapping, save the model. Entities and mapping files are successfully generated and stored in the specified folders:

Images_GenCodeMF

 

 

ExpandedToggleIcon        Updating the Database from the Model

This section explains how to update the database from the model that has been created using the Model-First approach.

To update the database from the model, perform the following sequence of actions:

1.Select Tools --> Entity Developer --> Update Database from Model.

Images_UpdateFromModel0

note Note

If the Recreate Database Tables check box is selected, the existing database tables will be dropped and created anew.

Select the Regenerate Storage check box to completely regenerate the whole Model Storage part and Mapping, including the description of storage columns and ends of relations.

In case of NHibernate, all the customization of Columns options and other mapping settings will be filled automatically using our engine, converting Conceptual model to the Storage model and Mapping between them. The engine considers entities, associations, and inheritances from the Conceptual model.

When working with NHibernate ORM this option is rarely used because NHibernate runtime does not require the Storage part and Mapping and can work without them. With NHibernate, this option should be used only if you want your Storage part to be always completed, and want it to be done automatically.

2.(Optional) Select the Regenerate Storage check box, if appropriate.
3.Click Next.
4.On the Set up data connection properties page, specify the connection parameters for your model and click Next.

Images_ChooseSchemasMF

5.On the Choose schemas page, select one or more schemas to be compared and click Next.

Images_ChooseChangeActions

6.On the Choose change actions page, select the appropriate action(s). Use the context menu of the root nodes to check or uncheck all added, dropped or changed objects. Click Next.

Images_ViewExecuteScript

7.On the View and execute script page, click Execute to execute the displayed scripts.
8.On the final window of the wizard click Finish.
9.To check that the process provided valid results, go View --> Entity Developer and select Database Explorer.

Images_DatabaseExplorer

note Note

In case you just want to generate a database object creation script, you can use Generate Database Script Wizard, for details see the Generate Database Script Wizard section.

 

ExpandedToggleIcon        Testing the Model

Certain test data must be entered in order to test the correctness of entities and mapping. A sample procedure for entering test data is given below.

Entering Test Data

To enter test data, perform the following sequence of actions:

1.In the Database Explorer window (see above), right-click dbo.Blogs and, from the shortcut menu, select Retrieve data.
2.In the Blog Title column of the Data tab, enter "dotConnect", as shown below.

Images_dbo.Blogs

3.In the Database Explorer window, right-click dbo.Posts and, from the shortcut menu, select Retrieve data.
4.Enter the test data, as shown below.

Images_dbo.Posts

Creating and Using a New Query

After the test data have been entered, the next stage is to create a new query and use it to test the validity of the model. Data returned on the query must be identical to those entered in the previous procedure.

To create and use a query, perform the following sequence of actions:

1.Go to Tools --> Entity Developer and select New Query.
2.From the Query type drop-down list of the Entity Developer Query toolbar, select HQL.
3.On the displayed Query.eqry tab, enter a new query, for example, "from Blog"
4.On the Entity Developer Query toolbar, click the Run Query button.
5.View the results of the query (shown below).

Images_QueryResults

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.