How we can create 3 tier architecture in ASP.Net MVC application with EF?

.net 3-tier c# entity-framework-6


With Entity Framework 6, I'm developing a new project (web application) in ASP.NET MVC 5. I've worked with ASP.Net web form applications in a three-tier architecture, but dealing with entity frameworks, models, and other concepts is baffling me. So, how can I create a three-tier design in MVC using EF and can I use entity framework with it?

2/10/2015 12:14:13 PM

Popular Answer

The answer is that you can use a 3-N tier design (or something similar).

An excellent partnership exists between entity framework and ASP.NET MVC. Even with the default ASP.NET MVC template, EF is deployed and utilized for user/role management (identity).

Models, Views, and Controllers make up an average ASP.NET MVC application. Briefly:

  • POCOs are models. The model contains the data and is in charge of ensuring the accuracy of the data. To provide an analogy, this is a component of both the data and application tiers. The data tier also contains the EF database context class.
  • Views are highly typed.cshtml files that create html (have model). This is the presentation tier, to use an analogy.
  • The processing of HTTP requests, obtaining the data model, and providing the model to the view are all the responsibility of the controller. As an example, this is a component of the application tier (business logic).

The controller typically receives a viewModel, verifies it, processes it, and returns an action result (view, partial view, JSON, file, etc.). In the process section, the controller can set up the entity framework database context and retrieve or save data from the database.

The repository/Unit of Work or service pattern is used in many ASP.NET MVC solutions because it's nearly always a good idea to "keep the controller as small as feasible".

An illustration of a common MVC application component for constructing an entity utilizing 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. 


public MyController
    private IMyService myService;

    public MyController(IMyService myService)
        this.myService = myService;

    // Showing create/edit form
    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
    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.


public class Something
    public int Id { get; set; }
    public string Name { get; set; }
    // .. more properties (can be decorated with validation attributes)

Models for presentation views

// 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)


@model SomeViewModel

<!-- Form -->

submit button

<!-- /Form -->
2/10/2015 1:17:06 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow