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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:59:52+00:00 2026-05-13T15:59:52+00:00

I have implemented a simple repository pattern for the Entity Framework in a web

  • 0

I have implemented a simple repository pattern for the Entity Framework in a web app.

I have several repositories which all subclass a base which has some common methods in

The base looked like this

public class BaseRepository<TEntity> : IRepository<TEntity>
{
    protected readonly RedirectsEntities Context;

    public BaseRepository()
    {
        Context = new RedirectsEntities();
    }

(RedirectsEntities is the EF datacontext, or whatever it’s called)

And I had a RuleRepository and a SiteRepository which subclassed it

However, this caused me problems when looking up a site and using that value to save to a rule

the error was

“ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker”

Presumably because each repository has a different instance of RedirectsEntities ?

So I found this question :
ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker

which suggests moving the datacontext to a seperate class, holding it in a a static variable

e.g.

public class DataContext
{
    private static RedirectsEntities _dbEntities;
    public static RedirectsEntities DbEntities
    {
        get
        {
            if (_dbEntities == null)
            {
                _dbEntities = new RedirectsEntities();
            }
            return _dbEntities;
        }
        set { _dbEntities = value; }
    }
}

and then my base repository constructor would look like this :

public BaseRepository()
{
     Context = DataContext.DbEntities;
}

So this appears to have solved my problem, but I am concerned that the scope of RedirectsEntities is now incorrect.

Can anyone comment on this?

  • 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-13T15:59:52+00:00Added an answer on May 13, 2026 at 3:59 pm

    In web applications the most common solution is to scope the context per http request. You initialize the context at the beginning of the request and dispose it at the end. During the request you can save the context in the session state.

    If you use a Inversion of Control (IoC) container you can have your container take care of saving the context during the request and supply it to your repositories.

    Update

    If you are not using a IoC I would create the datacontext at the beginning of the request and put it in the session state. Then I would change the constructor of your base repository to take a instance of your datacontext as a parameter. Then each time you create a repository you get the context from the session and supply it to your repository. And at the end of the request you get the context from the session and dispose it. This way, all your repositories will share the same context over one request.

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

Sidebar

Ask A Question

Stats

  • Questions 291k
  • Answers 291k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer here some ideas. A vanilla brute force algorithm would try… May 13, 2026 at 6:03 pm
  • Editorial Team
    Editorial Team added an answer Pipes that don't lead to the terminal don't have an… May 13, 2026 at 6:03 pm
  • Editorial Team
    Editorial Team added an answer There will be a difference, between a 200KB and a… May 13, 2026 at 6:03 pm

Related Questions

I am creating a sample, application to understand repository and factory method patterns together,
I am using a simple repository pattern and have objects with a LazyList such
I have spent some time now to solve a problem for which i have
Here is a subset of the Python grammar: single_input: NEWLINE | simple_stmt | compound_stmt
I have a Question object thats the root of the Question Aggregate. THe question

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.