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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:24:38+00:00 2026-05-30T13:24:38+00:00

I have an isolated problem with cached data in a web app, and understand

  • 0

I have an isolated problem with cached data in a web app, and understand that it’s cached due to the way the datacontext is reused. My Repository classes return IQueryable types, so I’m unable to refresh the datacontext by closing & reopening it. For example, my CustomerRepository has this datacontext constructor:

private CAClassesDataContext context = new CAClassesDataContext();

And all database interaction methods then use this context, e.g:

public IQueryable<Customer> Customers
{
     get { return context.Customers; }
}

If I try to use the context in a using block:

using(CAClassesDataContext context = new CAClassesDataContext())
{
...
}

I can’t access the related classes as lazy loading is used. I’ve tried adding:

context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer)

after the update but the problem still exists. How can I force LINQ to SQL to use the database data instead of the cached?

UPDATE:
Thanks to answers provided by Steve & Andrew I was able to resolve the issue. My respositories now have a constructor like this:

[Inject]
public CustomerRepository (CAClassesDataContext Context)
{
    context = Context;
}

And in my Ninject bindings added:

Bind<CAClassesDataContext>().ToSelf().InRequestScope();

My project uses a custom membership provider an I was unable to use the above technique due to the provider’s parameterless constructor. To get round this I added methods to the respository which get called before & after the databse lookup:

repository.CreateContext();
var cust = repository.GetAllCustomers().SingleOrDefault(c => c.Username == username);
repository.DisposeContext();
  • 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-30T13:24:39+00:00Added an answer on May 30, 2026 at 1:24 pm

    As @Andrew Stephens says, your context object should be being created and used on a per-request basis.

    Instead of hard-coding it to be created inside a repository, inject it into the repository via its constructor. If you’re using an IoC container you may have the ability to have the context managed on a per-request basis for you (Ninject provides this, for example), otherwise you can wrap your context in a request-scoped data store object using HttpContext.Items, as detailed in this article.

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

Sidebar

Related Questions

I have fully isolated this problem to a very simple play app I think
I have an isolated python script that simply captures data from Twitter's streaming API
My problem is The following: I have a windows application that stores a file
I have a memory leak that I have isolated to incorrectly disposed direct byte
I have a property in one of the classes I created that currently has
I have isolated the problem in the second block of code below (if you
I have done extensive testing with this and have isolated the problem to this.
OK, so I have isolated this down to a very specific problem. I was
I have isolated a problem we are running into down to a simple test:
For testing I have 1 isolated page - no masters, controls, …. My sessions

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.