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.
- Database Schema
- Model Approach
- 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.
Now, go to Visual Studio and add a project, as shown in the below image.
Then, opens a new project window.
Here, I am using the .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.
When clicking Manage NuGet Packages for Solution, then opens a dialog box and you can see the Entity Framework package and install the package in your application.
When we press the Install button, one more dialog box gets open.
After installation, you can see installed packages in the project solution.
Now, click on the project application and add ADO.NET Entity Data Model.
When you click the ADO.NET Entity Data Model tab, open a new dialog box and give a suitable name.
After pressing the OK button, a Model Content Entity Data Model Wizard dialog opens.
In the above image, we can see multiple model options but here, we choose EF Designer from the database model because we have available student records in our database.
Now, press the Next button and open the data connection wizard dialog box as shown in the below image.
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.
You can see the connection properties dialog box in the above image. Also here, you can see the 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.
Now, you can see the created connection string in the above image. Here, check the checkbox for the created connection string and save it into the application's web.config file. Then, press Next and open one more dialog box for Database Object and Setting.
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.
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.
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
Now, add a GridView control for displaying student records and Entity Data source from the Toolbox data tab and you must build a project solution.
Next, configure EntityDataSource.
Press the Next button and open the data selection dialog box.
Then, click the Finish button for configuring the Entity Data Source process.
After building the solution, open the web form on the browser for checking student information.
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.