Entity Framework isthe Microsoft Data Access Platform for .NET development. This course will introduce us to the procedure and behavior of using the Entity Framework from designing models, writing data access code, and integrating Entity Framework into ASP.NET MVC solutions.
What is database first approach?
Database first is an approach where we have the database already and we create entity models using entity framework.
Getting Started,
Step 1: Start by running Visual Studio Express 2012 for Web or Visual Studio 2012.
Visual Studio is an IDE(Integrated development environment), Just like we use Microsoft Word to write documents, we’ll use an IDE to create applications.
Step 2: Creating Application: Click New Project, then select Visual C# on the left, then Web and select ASP.NET Web Application. Name our project “CreateWebApplication” and then click OK.
Step 3: In the new ASP.NET MVC4 project dialog, click Empty, View engine is Razor, and then click OK.
![ASP.NET MVC4 Project]()
We have created ASP.NET MVC 4 Project.
Step 4: Open Microsoft SQL Server Management Studio. Then click on File menu, select Connect Object Explorer, give Server Name and then Connect.
Step 5 Create a new Database: Right click on Database, Click on New Database.
In New Database Dialog, give the Database Name, then OK.
![New Database]()
We have created a Database: StudyDB
![Database]()
Step 6: Create Table: Right click on Table, select New Table:
![Create Table]()
Design the table like this:
![table design]()
We have created StudentID, StudentName, Address, EmailID, PhoneNumber in the Students1 Table.
Step 7: Set Primary Key: We want StudentID to behave as a primary key, then select StudentID and right click; select set primary key.
![set primary key]()
Save this table with name Students1, right click on table and refresh it, then create dbo.Students1 in Table. Your table will look like the following:
![dbo.Students1]()
Now we will add data context of database which we have created earlier.
What is data context
Data context is a class using which we talk to the different tables in the database.
Open Visual Studio and in Solution Explorer, right click on our project, ADD New Item, select Data section from left tabs, click on ADO.NET Entity Data Model, give model name, and then Press ADD.
Click on Generate from Database, then Next, Click on New Connection, change data source i.e. Microsoft SQL server. Give name of server, then select database name. Here's the screenshot:
![New Connection]()
Click on Test Connection, if connection is successfully then click OK.
We can give any name to the model. We will give name “StudentNModel”. Select Table, and click on FINISH.
It will create entities of the tables which we selected in this step.
Step 8: Open Visual Studio, click on added data context (.edmx file):
![added data context]()
Click on Students1.cs, which is entity class of table students1, observe that all the columns in the students1 database table are added as properties of the class.
![Students1.cs]()
![code]()
Then save the project and build.
Step 9: Add Controller: Right click controller, click ADD select controller, Give name of controller, and in scaffolding options, we will select like the following image, then ADD.
Observe that, model class is selected as Students1 and data context class is selected as StudyDBEntities.
![ADD controller]()
It adds Views like the following:
![Views]()
Then in App_Start, click on RouteConfig.cs, change name of Controller.
![RouteConfi.cs]()
![Controller]()
Save and Build, and now RUN.
Step 10: Result:
![run]()
As we do not have any records in the Students1 table, so index view is showing no entries. We will go ahead and create few entries by clicking on 'Create' button. It redirects us to create view which looks like the following:
![result]()
If we fill data and click on create, it will add entry in 'Sudents1' table and will navigate to index view, which will look like the following:
![index]()
We can also click on Edit, Delete or Details links, to modify or view this record.
We have seen that we can use entity framework using ASP.NET MVC to communicate with database without writing a line of code.
Read more articles on ASP.NET MVC: