Model First Approach In Entity Framework


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:


  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 Add button it will prompt next window for Entity Data Model Wizard, in that select Empty Model because we are generating database from the model. After that click on Finish.

    select Empty Model

  9. After clicking on Finish button, it will open Entity Data Model Designer like the following image.

    Entity Data Model

  10. Right-click on empty space for adding new Entity or object or table for 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 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’.



    Repeat the Step 13 to add one more scalar property ‘City’.

    scalar property

    Now this is our final entity with three columns with primary key seen in the preceding image.

  16. Next, generating 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 database or use already created database.

    new connection

    After clicking New Connection, it will open Connection Properties window for selecting database instance and server name. Here I selected ‘JITU-PC’ and gave database name or selecting existing database. Here I provided   database name because I am going to create a new database with the name ‘ModelFirstApproach’.


    After clicking 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.


    It will show you the previous window with updated database name as in the following image:

    updated database

    Click on Next button. It will open the next step for selecting Entity Framework Version to use for creating the database. Here I selected version 6.0.

    Next button

    Click on Next button, it will show you the last step.

    Click on Next button

    In this model first approach the entity framework generates .sql file or script for creating the database. After that click on 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 in visual studio page like the following.


    generated script

  19. Now final step, Right click on opened script file and select Execute.

    Select Execute

  20. After selecting Execute, it will prompt you one window for Connecting to the Server. Enter Server name or select ‘JITU-PC’ and click on Connect.


  21. After clicking on Connect button it will execute the generated scripts.


  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 same record on screen.

  25. Add the following code on Program.cs file.


  26. Save the changes and Press F5 or Run the application, it will show you the following output:


    Checking the data is saved in database or not, see the below image.


    Great, your second Entity Framework approach - Model First Approach created successfully!


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