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.

Images_AddNewItemLC

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 LinqConnect Model.
5.Click Add.

Images_T_EF_DBF_CMW1

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

Images_EFT_MF_ModelSynchronization

7.On the Model Synchronization page, select Yes and click Next.

Images_TMF-ModelSyncSettings-LC

8.On the Model Synchronization Settings page, specify the appropriate settings for model synchronization and click Next.

Images_EFT_MF_SetupNamingRules

9.On the Set up naming rules page, specify the appropriate naming rules (if required) and click Next.

Images_TMF-ModelProperties-LC

10. On the Model properties page, leave the settings set to default and click Next.

Images_CreateModelWizardLinqCodeGenerationTemplate

11. 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.
12. Click the Add template button.

Images_CreateModelWizardLinqCodeGenerationTemplate2

13. 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).
14. After all the required templates are added to the list on the Choose Code Generation page of the wizard, click Next.
15. On the final window of the wizard click Finish.

The model is successfully created.

Creating a Class

This procedure explains how to create a class. For the purpose of this example, we shall create the Blog class.

1.In the active diagram area press INSERT.

Images_NewClass

 

2.Enter Blog in the class name field and press ENTER.
3.Right-click the created class and select New Property from the Add submenu of the shortcut menu.

Images_TMF-PropertyEditor-LC

4.In the Name field of the Property Editor dialog box, enter the name of the property (BlogID).
5.From the Type drop-down list, select long (System.Int64).
6.Ensure that the Entity Key check box is selected.
7.Select the Auto Generated Value check box. This step must be performed for all other entity keys in the model.
8.Click OK.
9.To create the second property (BlogTitle), right-click the created class and select New Property from the Add submenu of the shortcut menu.
10. In the Name box of the Property Editor dialog box, enter the name of the property (BlogTitle).
11. From the Type drop-down list, select string (System.String).
12. Leave other settings as they are and click OK.

All 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. 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_TMF-AssociationEditor-LC

2.From the Cardinality drop-down list select OneToMany.
3.From the Relation Class drop-down list in the End 1 area select Blog and leave other settings set to their default values.
4.From the Relation Class drop-down list in the End 2 area select Post and leave other settings set to their default values.
5.(Recommended) Ensure that the Delete Rule drop-down list is empty.
6.Click OK.

For information on how create other types of associations, see 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. 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, select the Author class and the RegularAuthor class while holding the CTRL button, right-click one of the selected classes and from the Add submenu of the shortcut menu, select New Inheritance.

Images_TMF-InheritanceEditor-LC

2.From the Inheritance Type drop-down list, select Table Per Hierarchy.
3.From the Base Class drop-down list, select Author.
4.From the Derived Class drop-down list, select RegularAuthor.
5.From the Discriminator Property drop-down list, select AuthorDiscriminator.
6.From the Inheritance Default drop-down list, select Author.
7.Set the Base Class Discriminator Value equal to 0.
8.Set the Derived Class Discriminator Value equal to 1.
9.Click OK.

This ends the procedure of inheritance creation.

For the purpose of this example, we create one more TPH inheritance with the Author base class and the GuestAuthor derived class. All settings in the Inheritance Editor are similar, with the exception that we set the Derived Class Discriminator Value equal to 2.

For more information on how to create inheritances of other types, see 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 - RegularAuthor;
oAuthor - GuestAuthor.

Below is the example of the resulting model:

Images_TMF-SampleModel-LC

 

 

ExpandedToggleIcon        Mapping and Code Generation Setup

This section explains how to set up mapping and code generation in LinqConnect 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 9 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 entities (for example, Entities) and a folder for context (for example, Context).
2.Navigate to the Model Explorer window, select the template and click to display the Properties window for the selected template.

Images_TemplatePropertiesLC

3.In the Output section of the model properties, click the Context 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 Context folder that was created in Step 1 of this procedure) and click OK.
5.Click in the Entities Output line and click the three-dotted line to open the Select Project Folder window.
6.In the Select Project Folder window, select the appropriate folder for mapping files (for example, the Entities folder that was created in Step 1 of this procedure) and click OK.
7.Select an appropriate value for the File Per Class property: if different folders are specified for the context and entities, the File Per Class parameter must be set to True.
8.Set the Generate Partial Class property to True define that for each class of the model a partial class should be generated, in which it will be possible to add code that won't be overwritten by the designer. The property can be used, if File Per Class is set to True.
9.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.
10. Ensure that you have a physical connection to the database server.
11. To generate code, save the model, or in the Solution Explorer docking window right-click the model and in its context menu select Run Custom Tool.

note Note

To generate code in the standalone application, click the Generate Code button on the application toolbar.

The context and entities are successfully generated and stored in the specified folders:

Images_TMF-SolutionExplorer-LC

 

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. We assume that there is a valid connection to the database server.

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.

If the Regenerate Storage check box is selected, the whole Model Storage part and Mapping, including the description of storage columns and ends of relations, will be completely re-generated.

Since LinqConnect model has explicit difference between Conceptual and Storage parts and Mapping between them, and Storage part and Mapping are required, this feature can be very useful, because it eliminates the need to create Storage part and Mapping manually in Model-First Approach.

2.(Optional) Select the Regenerate Storage check box, if appropriate.
3.Click Next.
4.The Set up data connection properties page of the wizard is displayed.

Images_updateFromModel1

note Note

This page is displayed only if the model was created within the Model First approach and no connection settings were specified. Besides, the appearance of this page may differ from the one pictured above, depending on the selected provider.

5.Setup data connection parameters. You can use the Test Connection button to verify the entered parameters.
6.Click Next.

Images_EFT_MF_NamingRules

note Note

The Set up naming rules page of the wizard is displayed, if the Regenerate storage check box is selected at Step 2 of this procedure.

7.On the Set up naming rules page, leave all the settings as they are and click Next.

Images_ChooseSchemasMF

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

Images_ChooseChangeActions

9.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_EFT_MF_ExecuteScript

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

Images_DatabaseExplorerEF

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 LINQ.
3.From the Model Explorer window, drag, for example, the Blog class and drop it onto the displayed Query.eqry tab. The query ("from b in Blogs select b") is formed.
4.On the Entity Developer Query toolbar, click the Run Query button.
5.View the results of the query (shown below).

Images_EFT_MF_FinalResult

 


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.