Entity Framework With ADO.NET Entity Data Model

In this article, we will learn how to use Entity Framework with ADO.NET Data Model in Visual Studio using C#.

What is an Entity Framework?                                                           

Entity Framework is the development of data-oriented applications using ADO.NET. Entity Framework solves problems in entity models, relationships, and business logic. Also, it works with data engines. 

This means Entity Framework is an Object-Relational Mapping (ORM) framework. An ORM structure provides database access and data operation process.

ORM framework automatically creates classes based on the data table and it can also create databases based on classes.

Entity Framework has three main parts.

  1. Database Schema
  2. Model Approach
  3. Code First

In this article, we will learn about the first option - Database Schema in Entity Framework.

How to work with Database Schema in Entity Framework?

When we already have a database available for application development, we should use a database schema in Entity Framework.

Let’s start with an example.

We already have student information records available in our database, as shown in the below image.

ADO.NET
Now, go to Visual Studio and add a project, as shown in the below image.

ADO.NET

Then, opens a new project window.

ADO.NET

Here, I am using .NET Framework 4.5. Select ASP.NET Empty Web Application and you can give a suitable name, as shown above.

If Entity Framework is not available in your solution explorer, you can add it through NuGet Package Manager.

ADO.NET

When clicking Manage NuGet Packages for Solution, then opens a dialog box and you can see Entity Framework package and install the package in your application.

ADO.NET

When we press the Install button, one more dialog box gets open.

ADO.NET

After installation, you can see installed packages in the project solution.

ADO.NET

Now, click on the project application and add ADO.NET Entity Data Model.

ADO.NET

When you click ADO.NET Entity Data Model tab, open a new dialog box and give a suitable name. 

ADO.NET

After pressing the OK button, a Model Content Entity Data Model Wizard dialog opens.

ADO.NET

In the above image, we can see multiple model options but here, we choose EF Designer from database model because we have available student records in our database.

Now, press the Next button and open data connection wizard dialog box as shown in the below image.

ADO.NET

In the above image, we can see the New Connection button. Here, create a new data connection string for data access from the database to the application, as shown in the below image.

ADO.NET  

You can see the connection properties dialog box in the above image. Also here, you can see ASO.NET data model provides two types of connection and we can use it as per our requirement. Here, I am using SQL Server authentication connection properties and selecting database name and test connection. Now, press the OK button.

ADO.NET

Now, you can see the created connection string in the above image. Here, check the checkbox for the created connection string and save into the application's web.config file. Then, press Next and open one more dialog box for Database Object and Setting.

ADO.NET

In the above image, we can see the created database object list. Here, you can select the data table. I am selecting only one student table in all available tables and giving the Model Namespace as shown in the above image.

Now, you can see created StudentModel.edmx in project solution explorer.

ADO.NET  

In this created model, you can see the Model class and diagram. Also, you can see a student class from our database student table with field properties.

ADO.NET

In this image, you can see the auto-generated student class. In this class, you can see ID, Name, Gender, MailID.

Now, add a web form to the project.

Project Name - Add - New Item - Web Form

ADO.NET

Now, add a GridView control for displaying student records and Entity Data source from Toolbox data tab and you must build a project solution.

ADO.NET

Next, configure EntityDataSource.

ADO.NET

Press the Next button and open data selection dialog box.

ADO.NET

Then, click the Finish button for configuring the Entity Data Source process.

ADO.NET

After building the solution, open web form on the browser for checking student information.

ADO.NET

Here, you can see student information from the data table as per our result from the database using a Database Schema Model in Entity Framework.

In this article, you saw how to use the Database Schema model and auto create related student class and context class for a data connection.

I hope you liked this article. In the next article, we will see database operations like insert, delete and edit/update using Entity Framework.