What is an Entity Framework?
The Entity Framework is the development of data oriented applications using ADO.NET. Entity framework solves problems in entities model, relationships, and business logic. Entity framework works with data engines.
This means Entity framework is an Object Relational Mapping (ORM) framework. An ORM structure provides a database access and data operation process. ORM stand for object relation mapping.
ORM framework automatically creates a classes based on 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 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 below image,
Then open new project window open as shown in below image,
Here I am using .Net framework 4.5. Select Asp.net Empty web Application and you can give suitable name as shown above.
If Entity Framework is not available in your solution explorer you can add it through NuGet Packege Manager as shown below
When click Mange NuGet Packages for Solution then open as shown below dialog box and you can see Entity Framework package and Install package in your application.
When we press install button then open one more dialog box as shown in the below image,
After installation you can see installed package in project solution as shown in below image,
Now click on project application and add Ado.Net Entity Data Model as shown in below image,
When you click Ado.Net Entity Data Model tab then open a new dialog box and give a suitable name as shown in the below image,
After pressing OK button then open a Model Content Entity Data Model Wizard dialog as shown in the below image,
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 Next button then open data connection wizard dialog box as shown in the below image,
In the above image we can see New Connection button. Here create a new data connection string for data access from database to application as shown in below image,
You can see connection properties dialog box in above image. Also here you can see Ado.Net data model provide 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 then press OK button.
Now you can see created connection string in above image. Here check checkbox for created connection string and save into application web.config file. Then press Next and open one more dialog box for Database object and Setting.
In the above image we can see created database object list. Here you can select 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 Model class and diagram. Also you can see student class from our database student table with field properties.
In this image you can see auto generated student class. In this class you can see ID, Name, Gender, MailID.
Now add a web form in project.
Project Name - Add - New Item - Web Form
Now add a gridview control for displaying student record and Entity Data source from Toolbox data tab and must build project solution.
Next configure EntityDataSource as shown in the below image,
Press Next button and open data selection dialog box.
Then click finish button for configuring the Entity Data Source process
After build solution and open web form on browser for check student information.
Here you can see student information from data table as per our result from database using a Database schema Model in Entity Framework.
In this article you can see how to use database schema model and auto create related student class, and context class for 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.