Working with Complex Types

Complex types are data types with internal structure but without Key properties. They are represented as light-green shapes on the diagram. They define a data structure composed of EdmSimpleType properties or other complex types.

ExpandedToggleIcon        Creating and Editing Complex Types

The main properties of complex types are:

Complex types do not have identities (or keys) and therefore cannot exist independently. They can only exist as properties of entity types or other complex types.
Complex types cannot participate in associations because complex type can't have an entity key.
A complex type property cannot have a null value, though the scalar properties of a complex type may each be set to null.
Complex type properties are available for modifying. Its constructor has no parameters.
Properties of the complex type have to be marked as attributes.

Complex types enable scalar properties to be organized within entities. The fields of these objects are mapped to the same record in the database table with the fields of the parent entity.

You can use complex types for a more precise presentation of the denormalized table structure and for the specific types returned from stored procedures and functions.

There are three ways to create a complex type. They are described below.

ExpandedToggleIcon        Creating Complex Types from Existing Entities

To create a complex type from an existing entity, select the properties from this entity and create a complex type based on them. For example, you have the Company entity and want to create a complex type describing address. For that purpose, perform the following steps:

Images_company-class

1.Click the Address property in the Company class, and then click the Country property while holding down the SHIFT key.
2.Drag the selected properties out of the Company class and place them onto the empty diagram space.
3.In the displayed Model Refactoring dialog box, click New complex type and then click OK.
4.The new complex type has been created. Its properties can be edited in the Type Editor dialog. To display Type Editor, double-click the complex type or select Edit from the complex type shortcut menu.

Images_typeeditor

ExpandedToggleIcon        Creating New Complex Type

To create a new complex type perform the following steps:

1.Right-click the Complex Types node in the Model Explorer and select New Complex Type item from the popup menu.
2.The new complex type is added to the model; we rename it to AddressType. Then we double-click the complex type to call the Type Editor dialog box and use the editor to create the set of properties: for the purpose of this example we create the same set of properties as in the previous subsection, they are AddressTitle, Address, City, Region, PostalCode, Country, Phone, and Fax.

note Note

To add a property to the newly-added complex type use one of the following options. Select the complex type and press the INSERT key - this will add a new property, or press ALT+P - this will call the Property Editor dialog allowing you to configure the new property immediately. When working with the Type Editor dialog, right-click in the Properties area of the dialog and select the Add Property command from the shortcut menu, or press the INSERT key. To edit a property, right-click it and select the Edit Property command from the shortcut menu or press ENTER to display the Property Editor dialog. To remove a property from the Properties area of the Type Editor dialog, right-click the property and select the Delete Property from the shortcut menu or press the DELETE key.

note Note

A complex type created in this way is not mapped initially which is indicated in the Error List window with the corresponding warning message: The complex type '<type name>' is not used by any object within the model.

3.The next step is to remove the properties Address, City, Region, PostalCode and Country from the Company class, and create instead of them a new AddressType property with Type equal to AddressType.
4.The final step is to map the complex type properties to the class. For this we double-click the class, in the Class Editor dialog box we locate the AddressType and in the Storage Column we set the corresponding columns for all of its properties:

Images_class-properties-EF

5.Now the complex type is mapped and the warning message is not displayed.

ExpandedToggleIcon        Returning Complex Type from Method

To return a complex type from the method, do the following:

a.Include the functions or stored procedures to the model during its creation. You can use stored procedures and functions which return a recordset and all of their input parameters can be null.

note Note

You can add methods to the Storage part either during the Create Model Wizard execution with the selected Database First option, or later on by dragging the required stored procedures and functions from the Database Explorer window.

b.After a stored procedure is added to the Storage part, right-click it and select the Create Method option; in the displayed message box click Yes to obtain metadata of procedure resultset.

The screenshot below shows that the return type of the method is equal to the complex type:

Images_SP-with-resultset-general-EF

c.The new complex type is created automatically. You can edit it as described in the previous section.

ExpandedToggleIcon        Deleting a Complex Type

To delete a complex type use one of the following options:

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

Entity Developer will then ask you whether you want to delete the object from the model too, and whether the object should be deleted from the conceptual model together with the corresponding storage model objects.

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.