Model First Approach In Entity Framework

Introduction

 
In this article we are going to learn Entity Framework Approaches. Before starting with learning the second approach read my first article to understand what is Entity Framework and Database First Approach.
  1. Database First Approach.
  2. Model First Approach.
  3. Code First Approach.

Model First Approach

 
The name specifies the functionality of this approach means creating model first using the Entity Framework Designer tool. We are just adding the entity model in our application, after generating the database from this model and using that model we are accessing the database objects and data.
 
The following are the steps for using the Model first approach in your application:
  1. Open Visual Studio 2013.
     
  2. File, then New Project.
     
  3. Select C# Temple and select Console Application name it ‘ModelFirstApproach’. Click OK.
     
  4. You can see the following screen:
     
    Temple
     
  5. Next, add Entity Data Model to design database.
     
  6. Right Click on Project, Add, then click New Item.
     
    New Item
     
  7. It will open a new window. Select data from the left side, then select ADO.NET Entity Data Model and name it ‘ModelFirst’ and finally click Add.
     
     
  8. After clicking on the Add button it will prompt the next window for Entity Data Model Wizard, in that select Empty Model because we are generating a database from the model. After that click on Finish.
     
    select Empty Model
     
  9. After clicking on the Finish button, it will open the Entity Data Model Designer like the following image.
     
    Entity Data Model
     
  10. Right-click on empty space for adding a new Entity or object or table for a database like the following way.
     
    Right-click, Add New, then click Entity.
     
  11. Entity
     
  12. After the above step, it will open the add entity window providing the entity name or table name ‘Employee’ and clicking OK.
     
    click on OK
     
    In the following image Create Key property means creating the primary key by default ID with the data type Int32.
     
    enter property name
     
  13. After clicking on the OK button you can see in Entity Data Model Designer one entity is created with the name ‘Employee’.
     
    Entity Data Model Designer
     
  14. Now, next, add more columns or scalar property. For that Right-click on Entity, Add New, then select Scalar Property like the following image.
     
    Add New Scalar Property
     
  15. Next, it will show in entity model with added scalar property ‘Propert1’ name, rename it to ‘EmployeeName’.
     
    Propert1
     
    EmplaoyeeName
     
    Repeat Step 13 to add one more scalar property ‘City’.
     
    scalar property
     
    Now, this is our final entity with three columns with the primary key seen in the preceding image.
     
  16. Next, generating a database from this Entity Model.
     
  17. Right-click on empty space and select Generate Database from Model.
     
    Select Generate Database from Model
     
  18. After clicking on Generate Database from Model, it will prompt the window of Generate Database Wizard for establishing the connection to create a database or use an already created database.
     
    new connection
     
    After clicking New Connection, it will open the Connection Properties window for selecting database instance and server name. Here I selected ‘JITU-PC’ and gave the database name or selecting the existing database. Here I provided the database name because I am going to create a new database with the name ‘ModelFirstApproach’.
     
    ModelFirstApproach
     
    After clicking the OK button it will prompt you with a message box with the message as in the following image, Click on Yes. If you select No, then the database is not created.
     
    message
     
    It will show you the previous window with the updated database name as in the following image:
     
    updated database
     
    Click on the Next button. It will open the next step for selecting an Entity Framework Version to use for creating the database. Here I selected version 6.0.
     
    Next button
     
    Click on the Next button, it will show you the last step.
     
    Click on Next button
     
    In this model first approach, the entity framework generates a .sql file or script for creating the database. After that click on the Finish button.
     
    entity framework generates
     
    See the bottom of the image, after clicking on finish it started adding the Entity Framework. Finally, you can see the generated script of the database on the visual studio page like the following.
     
    script
     
    generated script
     
  19. Now the final step, Right-click on the opened script file and select Execute.
     
    Select Execute
     
  20. After selecting Execute, it will prompt you to one window for Connecting to the Server. Enter the Server name or select ‘JITU-PC’ and click on Connect.
     
    Connect
     
  21. After clicking on the Connect button it will execute the generated scripts.
     
    executes
     
  22. Now just open the SQL Server Management Studio to check the database is created or not. See the following image, after opening the SQL Server the database is created.
     
    SQL Server Management
     
  23. Congratulations! You have created the database using the Entity Framework Model First Approach.
     
  24. Now, test the application inserting the record and getting the same record on-screen.
     
  25. Add the following code on Program.cs file.
     
    Program
     
  26. Save the changes and Press F5 or Run the application, it will show you the following output:
     
    Run
     
    Checking the data is saved in the database or not, see the below image.
     
    result
     
    Great, your second Entity Framework approach - Model First Approach created successfully!

Summary

 
I hope that beginners, as well as students, understood the Entity Framework model first approach with an example. If you have any suggestions regarding this article, then please contact me. Stay tuned for other approaches coming soon!