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_AddNewItemEFC

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 Entity Framework Core Model.

note Note

In the standalone application in the displayed New Model dialog select Devart Entity Framework Core Model and click Create.

5.Click Add.

CreateNewModel1MF_EFC

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

Images_CreateNewModel6EFC

7.On the Model properties page, define the settings of your model and click Next. Here you can select the target Entity Framework Core version from the ones, supported by the target .NET Framework of your project.

Images_CreateNewModel7EFC

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_AddTemplateEFC

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_NewClassTDA

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_PropertyEditorEFCMF

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_ColumnEditorEFCMF

 

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, enter the name of the property (BlogTitle).
11. From the Type drop-down list, select Varchar.
12. (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.
13. 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_AssociationEditorEFCMF

2.In the End 1 area, set Class to Blog and leave other settings set to their default values.
3.In the End 2 area, set Class to Post and leave other settings set to their default values.
4.In the Cardinality list select OneToMany.
5.(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.
6.(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.
7.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 (Flat inheritance).

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_InheritanceEditorEFCMF

 

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, from the Type drop-down list, select the appropriate type for the discriminator column. 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.

Repeat these steps to create the Author - GuestAuthor inheritance.

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.
Associations:
oBlog - Post, Blog - BlogRoll (one-to-many);
oBlog - Author (one-to-one);
Inheritances:
oAuthor - Regular Author;
oAuthor - Guest Author.

Below is the example of the resulting model:

Images_SampleModelMFEFC

 

ExpandedToggleIcon        Code Generation Setup

This section explains how to set up code generation in Entity Framework Core models.

For the purpose of mapping and code generation, you may need to create and define folders, into which the generated context and entities 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 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.

TemplatePropertiesEFC

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.To generate code, save the model.

Entity files are successfully generated and stored in the specified folders:

Images_GenCodeMF_EFC

 

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 Entity Framework Core, 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 Entity Framework Core, this check box is rarely used because Entity Framework Core runtime does not require the Storage part and Mapping and can work without them. With Entity Framework Core, 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_EFC

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

Images_ChooseChangeActionsEFC

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_ViewExecuteScriptEFC

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_DatabaseExplorerTDA

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 blog_tda.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_tda.Blogs

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

Images_tda.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.On the displayed Query.eqry tab, enter a new query, for example, "from b in Blogs select b"
3.On the Entity Developer Query toolbar, click the Run Query button.
4.View the results of the query (shown below).

Images_EFT_MF_FinalResult

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.