Entity Developer from Devart is a very powerful modeling and code generation tool, an ORM tool to be more precise. Entity developer lets you design your data access layer visually — at the drop of a hat. Since the data access layer generated by Devart contains automated and generated code, chances of error are minimal.

The official website of Entity Developer states: “Entity Developer can help you design models for various .NET ORMs in one unified interface. You can get support for all ORMs in one tool, or you may purchase a separate edition, working with one of the supported ORMs.” 

Referencehttps://www.devart.com/entitydeveloper/

This article talks about how you can synchronize the model and the database, i.e., update the model from the database as well as update the database from the model when working with Entity Developer. It starts with a brief discussion and why Entity Developer is a good choice as an ORM, and then the model first and database first approaches.

Prerequisites

To be able to work with the code examples demonstrated in this article, you should have the following installed in your system:

  • Visual Studio 2019 Community Edition
  • SQL Server 2019 Developer Edition
  • Entity Developer

You can download Visual Studio 2019 from here: https://visualstudio.microsoft.com/downloads/ 

You can download SQL Server 2019 Developer Edition from here: https://www.microsoft.com/en-us/sql-server/sql-server-downloads

You can download a copy of Entity Developer here: https://www.devart.com/entitydeveloper/

Create the Database Tables

We’ll create two database tables namely Orders and Customers. The following are the fields of the Orders table.

  1. OrderId
  2. OrderNumber
  3. OrderDate
  4. OrderQuantity
  5. CustomerId

Note that CustomerId is a foreign key in the Orders table. The following are the fields of the Customers table.

  1. CustomerId
  2. CustomerName
  3. CustomerEmail
  4. CustomerPhone

The following is the database script for creating the Orders and Customers tables.

Figure 1 below shows the database design for the two tables. 

customer and orders tables

Figure 1

Create a New ASP.NET Core Project

Assuming that the necessary software has been installed in your computer to be able to work with Entity Developer, follow the steps outlined below to create a new ASP.NET Core Web API project.

  1. First off, open the Visual Studio 2019 IDE
  2. Next, click “Create a new project” once the IDE has loaded
  3. Click “Create a new project” 
  4. Next, select “ASP.NET Core Web Application”
  5. Click the “Next” button
  6. Specify the project name and location – where it should be stored in your system
  7. Optionally, click the “Place solution and project in the same directory” checkbox.
  8. Next, click the “Create” button
  9. In the “Create a new ASP.NET Core Web Application” dialog window that is shown next, select “API” as the project template.
  10. Select ASP.NET Core 3.1 or later as the version.
  11. You should disable the “Configure for HTTPS” and “Enable Docker Support” options by disabling the respective checkboxes.
  12. Since we’ll not be using authentication in this example, specify authentication as “No Authentication”.
  13. Finally, click on the “Create” button to finish the process.

Create an Entity Data Model

In this section, we’ll explore how to create an Entity Data Model using Entity Developer. We’ll use both the two approaches database first and model first. To create an Entity Data Model using Entity Developer in Visual Studio 2019, follow the steps outlined below.

  1. Right-click on the project in the Solution Explorer Window
  2. Select Add -> New Item
  3. In the “Entity Developer: Create Model Wizard” window you can specify how the model should be created. You’ll have two choices – Database First and Model First
  4. Select the Database First Approach and click on Next to continue.
  5. In the next screen, specify the connection properties and click on the “Next” button to continue.
  6. Specify how the model should be generated in the next screen. Note that the “Generate from Database” option is selected by default.
  7. Since we’ll need the model to be created from the database, click on the “Next” button to continue.
  8. In the next screen, the database metadata is retrieved by the Entity Developer runtime. Here’s where you should specify the database objects that should be a part of your Entity Data Model. Select the Orders and Customers tables only.
  9. In the “Set up naming rules” screen you can specify naming rules for your entities.
  10. Click on the “Next” button to continue.
  11. In the next screen, specify the Context Namespace and click on the “Next” button to continue. 
  12. Now you can specify what your model diagram should contain. 
  13. Click on the “Next” button to continue. 
  14. Now you can choose the code generation templates if you want to. 
  15. Click on the “Next” button to continue.
  16. Click on the “Next” button again to continue.
  17. Click “Finish” to complete the process.

 

Your Entity Data Model using Entity Developer has been created. Here’s how your Entity Data Model would look like.

entity modelsFigure 2

Synchronizing Model and Database Data

When working with the Database-First approach you might want to make changes to the database and then make sure that the changes are reflected in the model so that the table schema in the database is in sync with that of the model. In this regard there are two approaches – you can either update the database from the model or update the model from the database.

Update Database from Model

Select the OrderNumber field, right-click and then click on Edit. Change the name of the field from OrderNumber to OrderNo. 

Figure 3

Next, right-click on the blank space anywhere in the Entity Data Model screen and click on “Update Database From Model…” as shown below.

updating database from model

Figure 4

After you click on the “Update Database From Model…” option, the following screen appears. 

pop up window

Figure 5

Once you click on Next, you will be presented with the following screen.

view and executing script

Figure 6

Click Execute to execute the scripts on your database

Update Model From Database

Let’s add a new column called CustomerAddress in the Customers table.

database file system

Figure 7

entity developer update wizard

 Figure 8

Click on the Recreate Model checkbox so that the model is recreated based on the newest table schema.

Click Next. 

selecting a schema

Figure 9

In the choose schemas screen, select the database schema of your database.

Click Next.

choosing change actions

Figure 10

Select the Customers and Orders tables (shown as checkboxes here) as change actions.

Click Next.

wizard finished updating

Figure 11

Click on the checkbox to add new entities to the existing diagram.

Click Finish.

Here’s how the updated model would look like:

Figure 12

Summary

Entity Developer from Devart helps you to become more productive as a developer – it includes support for working with ADO.NET Entity Framework, Entity Framework Core, Hibernate, LinqConnect, Telerik Data Access, and LINQ to SQL. Entity Developer is also built-in Devart ADO.NET data providers – here’s the link: https://www.devart.com/dotconnect/

You can get started using Entity Developer after downloading a trial version from here: https://www.devart.com/entitydeveloper/download.html



Source link

Write A Comment