Creating Model Using Database First Approach

The following walkthrough describes how to generate a model from the existing database, manually add and edit classes and components, generate C# code for the model.

We shall use the standard Northwind database. For more information on how to download and install this database, see here.

ExpandedToggleIcon        Prerequisites

In order to complete this walkthrough, you will need access to the database server you are using. You will also need a data provider to the corresponding server installed on your system. The Northwind database must be created on the server. In Microsoft Visual Studio, you should create a new project that will be used for the purpose of this walkthrough.

ExpandedToggleIcon        Creating a Model from the Existing Database

To create a model from your existing database, 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

note Note

In projects, targeting .NET Framework, you can find Devart EF Core Model in the Visual C# Items -> Data node.

In projects, targeting .NET Core or .NET Standard, you can find Devart EF Core Model in the Visual C# Items -> ASP.NET Core -> Data node.

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.

CreateNewModel1

6.Ensure that the Database First option is selected and click Next.

CreateNewModel2

note Note

This page may differ from the one pictured above, depending on which provider is selected.

7.On the Set up data connection properties page, set up the connection to the database.
8.(Optional) To check that the connection is valid, click Test connection. If the connection cannot be established, correct the entered data and repeat this step.
9.Click Next.

CreateNewModel3

10. On this page, select the Generate From Database option and click Next.

CreateNewModel4

11. On the Select database objects page, select the tables and/or views that are to be included in the model and click Next.

CreateNewModel5

12. On the Set up naming rules page, define the naming rules for your model and click Next.

Images_CreateNewModel6EFC

13. On the Model properties page, define the settings of your model. Here you can select the target Entity Framework Core version from the ones, supported by the target .NET Framework of your project. In the standalone application, you also need to select the target .NET Framework.
 
You can also set some model settings, affecting model generation from database on this page.
 

note Note

On the Model Properties page, you may preserve either full information about the column or only full information about the type to obtain a database-independent model using the Preserve columns details and Preserve columns SqlType and Default check boxes. After creating such a database-independent model, you can easily create a database on a different server using the Update to Database functionality.

14. Click Next.

CreateNewModel7

15. On the Choose Model Diagram Contents page, select, which entities should be displayed on the diagram. There are three options: All Entities (all entities are displayed on the diagram), Split by Schemas (entities of a schema are displayed on a separate diagram), or Custom (manually select the required entities).
16. Click Next.

Images_CreateNewModel7EFC

17. 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.
18. Click the Add template button.

Images_AddTemplateEFC

19. 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).
20. After all the required templates are added to the list on the Choose Code Generation page of the wizard, click Next.
21. On the final page of the wizard click Finish. This page also allows you to automatically load the required Entity Framework Core NuGet package and install it to your project.

The model is successfully created:

Images_Diagram_EFC

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 entities and context code will be saved, as well as other parameters available in the Properties window of the selected template.

The definition of output folders for context and entities 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 context (for example, Context) and a folder for generated entity classes (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 an 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.Click the Context Output line and click the three-dotted line to open the Select Project Folder window.
8.In the Select Project Folder window, select the appropriate folder for mapping files (for example, the Context folder that was created in Step 1 of this procedure) and click OK.
9.To generate code and mapping, save the model.

Entities and mapping files are successfully generated and stored in the specified folders:

Images_File_Gen_EFC

This completes the procedure for creating a model from the existing database.

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.