In this article, we are going to learn what is Entity Framework (EF)? How to implement it in our application and approaches of EF like the following:
- Database First Approach.
- Model First Approach.
- Code First Approach.
What is Entity Framework?
EF is a data access framework from Microsoft that helps to bridge the gap between data structures and objects in your application.
It automatically,
- Generates strongly-typed entity objects that can be customized beyond 1-1 mapping.
- Generates mapping/plumbing code.
- Translates LINQ queries to database queries.
- Materializes objects from data store calls.
- Tracks changes in generating updates/inserts.
In the above screenshot, you can see how one by one approach comes in EF. Now we are learning the first approach i.e. Database First.
Database First Approach
The name specifies the functionality of this approach means the database is already there and we are just adding the entity model in our application and using that model we accessing the database objects and data.
The following are the steps for using the Database first approach in your application:
- Open Visual Studio.
- File, then New Project.
- Select C# Temple and select Console Application name it ‘DatabaseFirstApproach’.
- Click on OK.
- You can see the following screenshot:
- Now add Entity Framework Nuget package.
- Right-click on the project and select Manage NuGet packages.
- You can see a window like the following screen:
In the above screen, you can see the Installed Package on the Project or search online, click on Installed.
Click on Accept, it will show the following installation screen:
After installation is finished, you can see it in the installed packages on the left side like the following:
Click on close.
- I have attached the database script with this demo just download it, create a database, and run that script.
- Now add Entity Data Model in your application: Right Click, then Add New Item.
- Here's the next window,
Select Data from the left side, then select ADO.NET Entity Data Model and name the model. After that click Add.
- After Clicking Add it will populate another window:
Here we are generating a model from the database, so select first and click the next button.
- Next comes the Entity Data Model Wizard for database selection.
Click on New Connection,
Select here server name, database name, and click on Test Connection for checking.
Now connection succeeded.
- Next, click on the OK button, you can see the following screen:
The above screen shows that we selected database name and connection string name in App.config file -> Click Next.
- After clicking it will show you the choose your database objects or table settings like the following:
- In the above screen, I selected all the database objects, I have only two database tables,
- Department
- Employee.
Next, click on the Finish button after clicking you can see the following screen:
In the above screen, you can see the Entity Data Model file with two entities we previously added Department and Employee. On the right side in the solution explorer, you can see in the boxes, first is Entity Framework and SQL Server references and next is the DB Context file generated and two database objects Department.cs and Employee.cs.
- The following screen shows the auto-generated entities file with DbContext inherited:
- Now, the next step is accessing data from this DBContext file using the objects Department and Employee.
- Open Program.cs file and write the following data access logic in that:
When you create an object of context class and access objects using (.), then it will show the list of database objects.
- You can access properties of that database objects like the following:
The above screen shows the access data from objects and showing in the command prompt. Now, save the changes and click on Run.
- You can see the following output:
Great, your first Entity Framework approach Database First Approach created successfully!
Summary
I hope that beginners, as well as students, understood Entity Framework database first approach with an example. If you have any suggestions regarding this article, then please contact me. Stay tuned for other approaches coming soon!