Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:44:14+00:00 2026-06-18T05:44:14+00:00

Out of the box, when you create a controller in MVC using Entity Framework,

  • 0

Out of the box, when you create a controller in MVC using Entity Framework, you’ll get the basic Get statements that pull data into your controller to display into your view. For example:

    public ActionResult Index()
    {
        var capsules = db.capsules.ToList();
        return View(capsules);
    }

The way I understand it, the best way to write MVC code is to put your data specific logic inside of a Models project. The controller then calls that method in whatever model class you have your logic in, retrieves the data, then sends it to the view. The above example just simply retrieves a list of capsules- very basic. I didn’t create a special “CapsuleModelContext” class to handle the capsule retrieval operation because it’s being handled in my edmx file.

I guess this all boils down to the question of whether or not I should have the dbContext instantiated in the Controller, or the Model’s context class. Out of the box, the dbContext is instantiated inside the Controller. Any advice to best practices here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:44:15+00:00Added an answer on June 18, 2026 at 5:44 am

    Please draw a distinction between your domain models and your view models. Your view models are not responsible for persisting data. Your view models exist simply as containers for entities that are passed to and from your views. Quite simply, they are a model of the view – not the persisted data that the view is designed to manipulate.

    With this in mind, then, you should use your controller to interact with whatever entity you are using to manipulate your persisted data (be it a repository or – more directly – a database context). The view model is used as a medium through which to communicate this data from your view to your controller, which, in turn maps your fields to whichever business entities it pertains and interacts with your chosen method of persistence. The view model and your business entity models should be ignorant of each other.

    The only logic your view model should contain (and this should, ideally, be kept to a minimum) is logic that is clearly only specific to that view model itself.

    I posted another answer to a similar question, which demonstrates a skeleton of how this works in practice:

    TextBoxFor() not generating validation markup

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using UAModalPanel to create a popover controller effect. I can get the
i am trying to figure out how to create a javascript alert box asking
There is a out of box resources in Zend Framework, when configures in the
I noticed out of the box that ELMAH logs a 404 not found for
I have a concrete5 site that works 'out of the box' in apache server.
I'm using some versioning techniques in web api that will basically call a controller
I'm using Codeigniter framework to create my web application. I have a login form
I'm using Codeigniter framework to create my web application. I have a login form
I am trying to create a small 20x20px div box, that when clicked opens
Am I correct to think that I have to create my controller by passing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.