Entity Developer from Devart is a modeling and code generation tool that lets you design your data access layer visually – at the drop of a hat. You can take advantage of Entity Developer to generate data access layer code automatically using one unified interface – as a result, chances of error creeping into your Data Access code are minimal.

In this article we’ll take advantage of Entity Developer to connect to and work with various ORM tools such as, EF Core, NHibernate, Telerik Data Access and LinqConnect. This article starts with a brief discussion on what ORMs are, why Entity Developer is useful, and how you can leverage Entity Developer to connect to and work with all these ORMs.

This article discusses the following points:

  • What is an Object Relational Mapper?
  • Create a new ASP.NET Core Project in Visual Studio 2019
  • Create the Database
  • Working with EF Core using Entity Developer
  • Working with NHibernate using Entity Developer
  • Working with LinqConnect using Entity Developer
  • Working with Telerik Data Access using Entity Developer

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 from here:

https://www.devart.com/entitydeveloper/

What Is an Object Relational Mapper?

Typically, an application has two perspectives, such as the Data Model and the Object Model. While the former is used to define how data would be stored in the database, the latter is used to represent the data in the presentation layer. However, the data model that resides in the database usually doesn’t match with the object model, i.e., how the data is represented in the user interface layer. Here’s where ORMs come to the rescue!

An Object Relational Mapper (i.e., an ORM, O/RM, and O/R mapping tool) is a framework that enables the data model in your database to be mapped with the objects in your applications and vice-versa while abstracting the object model from its relational or logical model.

Create a New ASP.NET Core Project in Visual Studio 2019

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.

Creating the Database

Now that the application is ready for use, let’s create the database. We’ll keep the database design simple for the sake of simplicity – we’ll use a database with just two tables with simple design.

Launch the SQL Server Management Studio and create a new database called Demo. Next, use the following script to create two tables named Products and Categories inside the Demo database.

Figure 1 shows how these two tables are related.

entities in database

Figure 1

We’ll refer to this database in the subsequent sections of this article.

Working With EF Core Using Entity Developer

In this section we’ll explore on how to create an Entity Data Model using Entity Developer. 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

Figure 2

This would launch the “Entity Developer: Create Model Wizard” window as shown below.

Figure 3

Here’s how you should specify how your model should be created. There are two options: Database First (this is selected by default) and Model First. In this example we’ll take advantage of the Database First approach.

  • Click on the “Next” button to continue.
  • In the next screen, specify the connection properties and click on the “Next” button to continue.

Figure 4

In the next screen you should specify what the model should contain. The “Generate from Database” option is selected by default as shown in the figure given below.

Figure 5

  • Since we’ll need the model to be created from the database, click on the “Next” button to continue.

In the next screen the database metadata is retrieved by the Entity Developer runtime and here’s how the database objects are selected.

Figure 6

  • De-select all options and then specify only the database objects you would like to be a part of the model. In our Demo database we’ve only two tables – Products and Categories. We’ll select both these tables here.

Figure 7

  • In the “Set up naming rules” screen you can specify naming rules for your entities.

Figure 8

  • Since we’ll not be making any change here, click on the “Next” button to continue.

The screen that is shown next is “Model properties” as shown in the figure below.

Figure 9

  • Specify the Context Namespace as “DemoModel” and click on the “Next” button to continue. In the next screen you can specify what your model diagram should contain. 

Figure 10

Note that the two database objects we selected earlier is displayed and the “All Entities” radio button is selected by default.

  • Since we’ll not be changing anything here, click on the “Next” button to continue. In the next screen you can choose the code generation templates if you want to.

Figure 11

  • Since we’ll not be modifying anything here, click on the “Next” button to continue.
  • In the next screen click on the “Next” button to continue.

Figure 12

  • Click “Finish” to complete the process.

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

Figure 13

Working With NHibernate Using Entity Developer

NHibernate is a popular, open-source, fast growing object-relational mapper that can be used in .NET and .NET Core applications to simplify data access. It is being actively developed and is used in many projects worldwide. Here’s the official home page: https://nhibernate.info/

Here’s a list of some of the striking features of NHibernate:

  1. Open-source
  2. Support for numerous databases (works with SQL Server, Oracle, DB2, the Firebird, MySQL, PostgreSQL, SQL Lite, etc.) 
  3. Support for logging and audit integration
  4. Flexible query APIs
  5. Provides support for ODBC and OLEDB drivers
  6. Supports bidirectional associations

To create an entity data model for NHibernate using Entity Developer, follow the steps outlined below.

  • Right-click on the project in the Solution Explorer Window
  • Select Add -> New Item

Figure 14

  • This would launch the “Entity Developer: Create Model Wizard” window. Here’s where you’ll be prompted to select how your model will be created. By default, the Database First option will be selected. Click Next to continue.
  • In the next screen set up the database connection properties and if the connection to the database is successfully established, click Next to continue.
  • Since our model will be generated from the database (the Generate From Database option is selected by default), click Next to continue.
  • Select the database objects you would want to be a part of the entity data model and click Next to continue.
  • In the next screen you can set up naming rules. Click Next to continue.
  • In the next screen you can specify the model properties and then click Next to continue.
  • You can choose the model diagram contents in the next screen and then click Next to continue.
  • Specify the code generation templates you would like to use in the next screen. The NHibernate template would be selected by default. Click Next to continue.

Figure 15

The entity data model will be successfully created.

Working With LinqConnect Using Entity Developer

LinqConnect is yet another tool from Devart – it is a light-weight, fast, simple, and easy to use LINQ to SQL compatible ORM product that can be used to connect to SQL Server, Oracle, MySQL, PostgreSQL as well as SQLite databases. Here’s the link to the official website: https://www.devart.com/linqconnect/

Here’s a list of some of the striking features of LinqConnect at a quick glance:

  1. Complex type support
  2. Advanced data fetching options
  3. Configurable compiled query caching
  4. High performance
  5. Multi database support
  6. Support for code-first, model-first and database-first approaches

To create an entity data model using LinqConnect follow these steps:

  1. Right-click on the project in the Solution Explorer Window
  2. Select Add -> New Item

Figure 16

Next, follow the steps outlined in the section “Working with EF Core using Entity Developer”.

Working With Telerik Data Access Using Entity Developer

Telerik Data Access solves the object-relational impedance mismatch that exists between a relational database management system and the object models in your application. In other words, the Telerik Data Access ORM solves the impedance mist=match that exists between the object and relational models. Here’s the link to the official website: https://docs.telerik.com/data-access/data-access-introduction

To get started with Telerik Data Access in ASP.NET Core web application, you can install the following NuGet packages.

  • Telerik.DataAccess.Fluent
  • Telerik.DataAccess.Core
  • Telerik.DataAccess.Fluent.Sample
  • Telerik.DataAccess.Web
  • Telerik.DataAccess.Profiler

Alternatively, you can download a copy of Telerik Data Access MSI installer from your Telerik account. If you don’t have an account, you can create one for free here: https://www.telerik.com/login/v2/telerik

The following are the striking features of Telerik Data Access:

  1. Support for mapping multiple data persistence models to a single storage schema
  2. Support for caching with optimizations
  3. High performance
  4. Support for LINQ
  5. Support for Stored Procedures

To access data for CRUD operations using Telerik Data Access, you should create a data context class that extends the OpenAccessContext class.

To create an entity data model using Telerik Data Access, select the “Devart Telerik Data Access Model” in the “Add New Item” window. Next, follow the steps outlined in the section “Working with EF Core using Entity Developer”.

Summary

Object Relational Mappers (ORMs) have emerged to solve the problem of impedance mismatch between the relational and the object models in an application. ORMs help increase development productivity, provide database independence, and also abstract calls to the underlying database. This helps you to focus on the business logic of the application since the data access code can be automatically generated. Entity Developer is a popular tool from Devart with many interesting features – you can take advantage of it to design entity data models easily.



Source link

Write A Comment