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

  • Home
  • SEARCH
  • 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 8924031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:17:41+00:00 2026-06-15T07:17:41+00:00

I am new to the MVC concept and seperating Data from the controller logic.

  • 0

I am new to the MVC concept and seperating Data from the controller logic. I am using Linq to Sql – and now I create a new instance of my DbContext , then use that to query the db from the controller. I have to do a lot of the same queries in different spots in my code – so my question is – Am I able to create a seperate class in my Model folder , create a new instance of my dbcontext in there, and create functions that query db in the class , make those functions public and then call them from any controller I want? I guess its a 2 part question , am I able to do this , and is this the preferred way to connect to db or do I just write all seperate code in different controllers?

  • 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-15T07:17:42+00:00Added an answer on June 15, 2026 at 7:17 am

    If you have to do similar/same queries many times, I recommend you compile your LINQ queries(The link is for LINQ to Entities, but basically the same idea for LINQ to SQL). It has much better performance from calling the query repeatedly. You can do something similar to the below snippet:

    static readonly Func<DbContext, IQueryable<T>> compiledQuery = CompiledQuery.Compile<DbContext, IQueryable<T>>(/*your query goes here*/);
    using (DbContext context = new DbContext())
    {
        IQueryable<T> results = compiledQuery.Invoke(context);
    }
    

    Basically you can just put these compiled queries in your DbContext class and then call it using the using statements.

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

Sidebar

Related Questions

Here's a little background on my solution: ASP.Net MVC app Using Linq-to-SQL with table-per-hierarchy
I am new to MVC, repository concept and dependency injection. My repository and DAL
I'm planning to design simple website using ASP.NET MVC new technology. Many concepts are
I've been doing alot of testing with my new MVC App and now I'm
I am new with MVC I am trying to create a small product form
I'm new to the MVC concept, and somewhat new to PHP. Question 1 Before
I am new to the concept of MVC and multi-tiered web architecture. I developing
I am still new at PHP and the MVC concept. I have been trying
I'm developing a web application using ASP.NET MVC (I'm new to the framework and
Im completely new to Zend Framework and the concept of MVC as well. What

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.