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 6758709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:50:00+00:00 2026-05-26T13:50:00+00:00

I created a simple CodeFirst (Entity Framework 4.1) context and then, using the right

  • 0

I created a simple CodeFirst (Entity Framework 4.1) context and then, using the right mouse button on the controller folder said : “Add Controller”, “Entity Framework”, picked one of my simple codefirst entities I created and the associated context. The controller code it genreated is as follows below.

What has me confused is why the Context is created as a private object in the Controller class. I would think that it should be create in a using() so that at the end of the method it could be disposed properly.

What am I missing here? I’m sure the guys who wrote this no what they are doing.

Thanks

namespace Web.Controllers
{ 
    public class TaskInfoController : Controller
    {
        private TaskContext db = new TaskContext();

        //
        // GET: /TaskInfo/

        public ViewResult Index()
        {
            var taskinfos = db.TaskInfos.Include(t => t.TaskGroup);
            return View(taskinfos.ToList());
        }
  • 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-05-26T13:50:01+00:00Added an answer on May 26, 2026 at 1:50 pm

    Well, this allows for atomic commits, but I agree, this is not the proper way. I use the Repository/Service/UnitOfWork pattern with MVC3 and EF and it works great. Search SO for it, there is a ton of info on it. In the meantime, move it into a using statement inside the controller action.

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

Sidebar

Related Questions

I'm using Code-first and entity framework in my .NET MVC 3 application, I created
Could someone help me on this, I have created simple web services using axis2
I have been using Entity Framework CTP with Code-First as in this tutorial by
I have created simple javascript widget where login happens using Facebook Single Sign On.
I've created simple ROR app on Heroku's server and I want to add product
I'm building an ASP.NET MVC 3 site using the code-first Entity Framework 4 approach.
When using the CTP 5 of Entity Framework code-first library (as announced here )
Framework: I'm using using MVC 3 + EntityFramework 4.1 Code-First. Concept: One Legislation entity
I created simple Silverlight Application using the web site host of the VS2010 (
I'm using Entity Framework 4.1's code first approach to map a class hierarchy to

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.