Working with Enums

Entity Developer for NHibernate supports the use of enum types in models.

Enumerated type is a distinct type that consists of a set of named constants. The NHibernate enum type definitions reside in the conceptual layer. The enum type has an underlying type (which can be SByte, Byte, Int16, Int32, or Int64; Int32 is used as the default underlying type if none has been specified).

Enumerated types can have zero or more members. A defined member must have a unique name (note that names are case-sensitive) and may have a value. If the value for the member is not specified, then the value is calculated based on the value of the previous member (through incrementing by one) or set to 0 if there is no previous member. If the value for a given member is specified, it must be in the range of its enum underlying type. Values can be specified only for some of the members; several members can have the same value.

Note also that, when Updating Database from Model is performed within the Model-First approach, for an entity with a property whose type is set to Enum, the type of the corresponding column that is created in the database is assigned based on the underlying type of the enum.

 

ExpandedToggleIcon        Adding a New Enum Type to a Model

To add a new enum type to a model and to display it on the diagram surface, perform the following sequence of actions:

1.In the Model Explorer window, right-click the Enum Types node and select the New Enum Type command from the context menu or, alternatively, click the New Enum Type button on the toolbar of the Model Explorer window, or on the Model toolbar, or just press ALT+N (using this keyboard shortcut adds a new enum type both is the Model Explorer window and on the diagram area). The Enum Type Editor dialog box is displayed:

Images_EnumTypeEditor

2.In the Name box, enter the name of the enum type being created. If this is an external enum (i.e. is already defined in your application), enter the full name including the enum namespace.
3.From the Access drop-down list, select either Public, or Internal.
4.From the Underlying Type drop-down list, select the required underlying type.
5.(Optional) To indicate that the enum type can be treated as a set of flags, select the Is Flag check box.
6.(Optional) To indicate that the enum type is external, and its definition should not be generated by Entity Developer, select the Is External check box.
7.(Optional) In the Members table specify names and values of the members for the enum type being created.
8.Click OK.

The newly created enum type is displayed in the Enum Types node of the Model Explorer window. If needed, drag it to the diagram area.

 

ExpandedToggleIcon        Storing Enum Property Values in Database

NHibernate allows storing values of properties, having enum type, in the database in two ways: as string values (enum member names are used) or as integer values (enum member values or default values are used). By default, they are stored as integer values. You can change how they are stored model-wide by setting the Default Enum Storage Mode property for the model in Model Properties or in the Model Settings dialog box.

If you want to override this model-wide setting for some class or complex type property, you can change the value of its Enum Storage Mode property in the Properties window.

 

ExpandedToggleIcon        Adding Enum Type Members

To add a member of an existing enum type, perform the following sequence of actions:

1.Either in the Model Explorer window or on the diagram surface, right-click the required enum type.
2.From the shortcut menu, select the New Member command.

A new member with default properties is added to the selected enum type.

 

ExpandedToggleIcon        Modifying Existing Enum Types

To modify a member of an existing enum type, perform the following sequence of actions:

1.Either in the Model Explorer window or on the diagram surface, double-click the required enum type.
2.In the Enum Type Editor dialog box (see the picture above) change one or more of the required parameters as described in Steps 2-6 of the Adding a New Enum Type to a Model subsection above.
3.Click OK.

The selected enum type is successfully modified.

ExpandedToggleIcon        Deleting Existing Enum Types

To delete an existing enum type use one of the following options:

in case you want to delete the enum type from the diagram area only, select it in the diagram area and press SHIFT+DELETE;
right-click the required enum type on the diagram area or in the Model Explorer, and select Delete in the context menu or select the required enum type and press the DELETE key.

Entity Developer will then ask you whether you want to delete the object from the model too.

If the enum type being deleted is assigned as the type of a property of an entity in the model, after the standard confirmation dialog the following warning is displayed:

Images_EnumDeleteWarningNH

Using this dialog, select the appropriate action.

 

ExpandedToggleIcon        External Enums

Entity Developer can work with external enums. These are enums that are already defined in your project, and they should not be generated by Entity Developer. For such enums you may omit member definition. When you specify their name, you need to enter the full name including the namespace, containing the enum. To define an enum as external, select the Is External check box in its editor or set its Is External property value to True in the Properties window.

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.