Database First Approach In Entity Framework

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:
  1. Database First Approach.
  2. Model First Approach.
  3. 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,
  1. Generates strongly-typed entity objects that can be customized beyond 1-1 mapping.
  2. Generates mapping/plumbing code.
  3. Translates LINQ queries to database queries.
  4. Materializes objects from data store calls.
  5. Tracks changes in generating updates/inserts.
approach comes in EF
 
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:
  1. Open Visual Studio.
     
  2. File, then New Project.
     
  3. Select C# Temple and select Console Application name it ‘DatabaseFirstApproach’.
     
    Console Application
     
  4. Click on OK.
     
  5. You can see  the following screenshot:
     
    Program.cs page
     
  6. Now add Entity Framework Nuget package.
     
  7. Right-click on the project and select Manage NuGet packages.
     
    Manage NuGet packages
     
  8. You can see a window like the following screen:
     
    NuGet packages
     
    In the above screen, you can see the Installed Package on the Project or search online, click on Installed.
     
    click on Installed
     
    Click on Accept, it will show the following installation screen:
     
    Click on Accept
     
    After installation is finished, you can see it in the installed packages on the left side like the following:
     
    installed packages
     
    Click on close.
     
  9. I have attached the database script with this demo just download it, create a database, and run that script.
     
  10. Now add Entity Data Model in your application: Right Click, then Add New Item.
     
    Add New Item
     
  11. Here's the next window,
     
    Entity Data Model
     
    Select Data from the left side, then select ADO.NET Entity Data Model and name the model. After that click Add.
     
  12. After Clicking Add it will populate another window:
     
    Data Model
     
    Here we are generating a model from the database, so select first and click the next button.
     
  13. Next comes the Entity Data Model Wizard for database selection.
     
    Entity Data Model Wizard
     
    Click on New Connection,
     
    Click on New Connection
     
    Select here server name, database name, and click on Test Connection for checking.
     
    Select here server name
     
    Now connection succeeded.
     
  14. Next, click on the OK button, you can see the following screen:
     
    Click next
     
    The above screen shows that we selected database name and connection string name in App.config file -> Click Next.
     
  15. After clicking it will show you the choose your database objects or table settings like the following:
     
    table
     
  16. In the above screen, I selected all the database objects, I have only two database tables,
     
    1. Department
    2. Employee.

    Next, click on the Finish button after clicking you can see the following screen:
     
    Click on Finish button
     
    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.
     
  17. The following screen shows the auto-generated entities file with DbContext inherited:
     
    code
     
  18. Now, the next step is accessing data from this DBContext file using the objects Department and Employee.
     
  19. Open Program.cs file and write the following data access logic in that:
     
    data access logic
     
    When you create an object of context class and access objects using (.), then it will show the list of database objects.
     
  20. You can access properties of that database objects like the following:
     
    employee address
     
    click on Run
     
    The above screen shows the access data from objects and showing in the command prompt. Now, save the changes and click on Run.
     
  21. You can see the following output:
     
    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!