I am creating a new project (web application) in ASP.NET MVC 5 with Entity framework 6. I have worked in 3 tier architecture with ASP.Net web form application but i am confusing to deal with entity framework, models etc. So, how can i build 3 tier architecture in MVC with EF and it is feasible to use with entity framework ?
-
MVC naturally consists of 3 layers which are Model, View and Controller, EF is an ORM that sits between the DB and the C# code and provides data manipulation or data access translations and it has nothing to do with the architecture. The models are one layer in the MVC arc but EF has nothing to do with it. – Transcendent Feb 10 '15 at 12:18
-
I'll go back later to this question with my own answer. I'm working on a small project of this type. – carloswm85 Oct 07 '22 at 19:50
3 Answers
Yes you can implement a 3/N tier architecture (or something similar).
ASP.NET MVC has a great collaboration with entity framework. EF even is installed and used for users/roles management (identity) in default ASP.NET MVC template.
A typical ASP.NET MVC application consists of Models, Views and Controllers. Briefly:
- Models are POCOs. The model encapsulate data and it's responsible for data validity. For analogy - this is a part of application tier and a part of data tier. The data tier include also EF db context class.
- Views are .cshtml files which produce html and can be strongly typed (have model). For analogy - This is the presentation tier.
- Controllers are responsible for processing of HTTP requests, retrieving data model and passing the model to the view. For analogy - This is a part of application tier (business logic).
Typically the controller will receive some viewModel, validate it, process it, and return some action result (view, partial view, JSON, file, etc.). In the process part the controller can initialize entity framework context, and gets or save data to database through the EF db context etc.
It's almost always a good idea to "keep the controller as thin as possible", so many ASP.NET MVC solutions use repository/Unit of Work or service pattern.
Example for some typical part of MVC application for creating some entity using services:
Services
// Connect to database through entity framework db context.
public interface IMyService
{
MyDbContext DbContext { get; set; }
IQueryable<Something> GetSomething( ... query params ... );
void CreateSomething(Something model);
// etc.
}
Controller
public MyController
{
private IMyService myService;
public MyController(IMyService myService)
{
this.myService = myService;
}
// Showing create/edit form
[HttpGet]
public ActionResult CreateSomething()
{
// Create Empty view model. new SomeViewModel(); for example.
// Get some nomenclatures for dropdowns etc. through the service (if needed).
// return View(viewModel);
}
// Processing the post request
[HttpPost]
public ActionResult CreateSomething(SomeViewModel viewModel)
{
// Check validity of viewModel (ModelState.IsValid)
// If not valid return View(viewModel) for showing validation errors
// If valid map the viewModel to Model (through AutoMapper or custom mapping)
// Call myService CreateSomething method.
// Redirect to page with details.
}
}
Model
public class Something
{
public int Id { get; set; }
public string Name { get; set; }
// .. more properties (can be decorated with validation attributes)
}
Presentation View Models
// View model for Something class. Including only properties related to the current UI.
public class SomeViewModel
{
public int Id { get; set; }
// .. more properties (can be decorated with validation attributes)
}
View
@model SomeViewModel
<!-- Form -->
@Html.ValidationForModel()
@Html.EditorForModel()
submit button
<!-- /Form -->

- 4,858
- 2
- 31
- 40
-
As par your article, I don't need to create 3 tier architecture, The combination of MVC and EF itself its become a 3/N tier architecture. Right ? – Mitesh Antala Feb 10 '15 at 13:48
-
The combination of MVC and EF itself become something very similar to 3-tier architecture but not exactly the same (in classic meaning of 3-tier architecture). I would recommend using of the described architecture but I can't know if you need this or 3 tier architecture in the classic meaning of the term. – Viktor Bahtev Feb 10 '15 at 14:27
Yes you can implement a 3 tier architectur:
- Tier (Presentation): Views (this is what the V stands for in MVC
- Tier (Logic): Usually the Models and some Helperclasses (this is what the M stand for)
- Tier (Data): Whit the help of the Entity Framework you could create a database from your models.
Here's a tutorial on how to use the Entity Framework with ASP.NET MVC.

- 2,133
- 3
- 25
- 45
Here is how you can implement 3-Tier:
Presentation layer include (MVC)
Business Logic Layer will include (C# programming - a dll)
Data access layer will include (C# programming with entity frameworkor- a dll)
Business Object Layer will include (Entity Framework models)
Ref: http://www.codeproject.com/Articles/841324/ASP-NET-MVC-Web-App-on-Tier-for-Beginners

- 29
- 2
-
This is a good answer, and the provided link seems to be pretty well done. But this answer need to be improved. – carloswm85 Oct 07 '22 at 16:22