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 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.
- After clicking on Finish button, it will open Entity Data Model Designer like the following image.
- 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.
-
- 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 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 the Step 13 to add one more scalar property ‘City’.
Now this is our final entity with three columns with primary key seen in the preceding image.
- Next, generating 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 database or use already created database.
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:
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.
Click on Next button, it will show you the last step.
In this model first approach the entity framework generates .sql file or script for creating the database. After that click on 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 in visual studio page like the following.
- Now final step, Right click on opened script file and select Execute.
- 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.
- After clicking on 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 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 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 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!