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.
- Database First Approach.
- Model First Approach.
- 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:
- Open Visual Studio 2013.
- File, then New Project.
- Select C# Temple and select Console Application name it ‘ModelFirstApproach’. Click OK.
- You can see the following screen:
- Next, add Entity Data Model to design database.
- Right Click on Project, Add, then click New Item.
- 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.
- 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.
- After clicking on the Finish button, it will open the Entity Data Model Designer like the following image.
- 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.
- After the above step, it will open the add entity window providing the entity name or table name ‘Employee’ and clicking OK.
In the following image Create Key property means creating the primary key by default ID with the data type Int32.
- After clicking on the OK button you can see in Entity Data Model Designer one entity is created with the name ‘Employee’.
- Now, next, add more columns or scalar property. For that Right-click on Entity, Add New, then select Scalar Property like the following image.
- Next, it will show in entity model with added scalar property ‘Propert1’ name, rename it to ‘EmployeeName’.
Repeat Step 13 to add one more scalar property ‘City’.
Now, this is our final entity with three columns with the primary key seen in the preceding image.
- Next, generating a database from this Entity Model.
- Right-click on empty space and select Generate Database from Model.
- 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.
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’.
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.
It will show you the previous window with the updated database name as in the following image:
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.
Click on the Next button, it will show you the last step.
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.
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.
- Now the final step, Right-click on the opened script file and select Execute.
- 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.
- After clicking on the Connect button it will execute the generated scripts.
- 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.
- Congratulations! You have created the database using the Entity Framework Model First Approach.
- Now, test the application inserting the record and getting the same record on-screen.
- Add the following code on Program.cs file.
- Save the changes and Press F5 or Run the application, it will show you the following output:
Checking the data is saved in the database or not, see the below image.
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!