Introduction
In our first three articles, we learned a lot about MVC, from its definition to its use, from creating an application to connecting the MVC application with a database using various techniques.
In the very last part of the series we learned how to connect our MVC application with an existing database using Entity Framework.
This article focuses on connecting our MVC application with a database using the CodeFirst approach, in other words one of the features that Microsoft's Entity Framework provides.
Our Roadmap
Just as a reminder of our full roadmap towards learning MVC, see:
- Part1: Introduction to MVCarchitecture and Separation of Concerns.VCarchitecture and Separation of Concerns.
- Part 2: Creating MVC Application fromscratch and connecting it with database using LINQ to SQL.
- Part 3: Connecting the MVC Application with the help of EntityFramework DB-First approach.
- Part 4: Connecting the MVC Application with the help of EntityFramework Code-First approach.
- Part 5: Implementing Repository Pattern in MVC Application with EntityFramework.
- Part 6: Implementing a generic Repository Pattern and Unit Of Work pattern in MVC Application with EntityFramework.
Pre-requisites
The following are pre-requisites for this article:
- We have a running sample application that we created in the third part of the article series.
- We have the Entity Framework 4.1 package or DLL on our local file system.
- We understand how a MVC application is created.
Code-First Approach
To do a domain driven design, the Entity Framework introduced EF 4.1 Code First. In the Code First approach, we focus on the domain design or entities/POCO classes first and create classes as per our model requirement. We do not have the database of the application, rather we create the database automatically from code after defining our domain. The database created perfectly matches with the domain we design, so we need to be very conscious and keen in designing our domain model. It feels exciting to see the database created on the fly using our entities and XML configuration, without even opening a database server.
No matter, you are not an expert in databases, if you are a C# developer then just focus on your model/class creation. The Entity Framework will relieve you of the headache of creating and managing databases.
Procedure
Step 1: Open the MVC application that we created in Learning MVC-Part 3 in your Visual Studio.

We can clearly see and remember what we used to connect our MVC application to the database using Entity Framework, yes it was a edmx class and our Model.tt classes generated from the edmx classes.
Step 2: We don't need an existing database, so you can delete the already created database for our Part 3 application (if created).
Step 3: We don't need edmx files now, so let's clean our application, wipe out all these classes. Just delete the EFDataModel.edmx, Model1.Context.tt and Model1.tt files. Now please do not run the application, it will give compiler errors , since we were using those classes ;-), Our Solution will look like:

Our old solution had the UserList class in the Models folder, I have only changed the name of the class for differentiating it with the previous application, and readability as was in the first part.
Step 4: As simple as that, just add a class to your solution, and name it MVCDBContext.cs as shown in the following image:

Step 5: Just add the System.Data.Entity DLL as a reference to the solution if not already added.
Step 6: Use the namespace System.Data.Entity in our DBContext class, and inherit the new class from the DBContext class, as in the following:

DbContext Class: According to the MSDN, the DbContext class is conceptually similar to ObjectContext .To define it, the ObjectContext class is the part of the core EF API in the Microsoft .NET Framework 4 and this is our hero class that allows us to perform queries, change tracking and update the database using the strongly typed classes that represent our model (entity class). The DbContext is a wrapper around ObjectContext that exposes the most commonly used features of ObjectContext as well as provides some simpler "shortcuts" for tasks that are frequently used but are complicated to code directly with ObjectContext that is a simplfied alternative to ObjectContext and is the primary object for interacting with a database using a specific model.
Step 7: Add a DBSet property to the DbContext class that we created, as in the following:
- public DbSet<User> Users { get; set; }








Mukesh ShahPosted Jun 25, 2021, 4:05 PM
Nice article sir
MohitPosted Jun 7, 2016, 10:33 AM
On running the code at below line of Index() method of Controller: var userList = from user in dbcontext.Users select user; Following exception occurs: An exception of type 'System.Data.Entity.Core.MetadataException' occurred in EntityFramework.dll but was not handled in user code Additional information: Unable to load the specified metadata resource. Can you please assist ASAP.
Sr KarthigaPosted Mar 2, 2016, 7:25 AM
good one
Sr KarthigaPosted Mar 2, 2016, 7:25 AM
Nice explanation
Kiran Kumar TalikotiPosted Sep 5, 2013, 7:57 AM
Nice Article sir.Usefull
Mahesh ChandPosted Jul 22, 2013, 9:20 AM
Nice! Love the hero images :)