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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:36:48+00:00 2026-05-22T21:36:48+00:00

This is in continuation of two ongoing problems I’m facing: Problems trying to attach

  • 0

This is in continuation of two ongoing problems I’m facing: Problems trying to attach a new EF4 entity to ObjectContext while its entity collection entities are already attached and EF4.0 – Is there a way to see what entities are attached to what ObjectContext during debugging? I’m using this space to ask another somewhat complicated question, and I don’t want to make a huge, ultra long question out of my other threads.

So, a quick rundown:

I have incoming form data which is bound to a DTO. I want to map the DTO to an Entity (a Game entity). The wrinkle is that the Game contains a EntityCollection which I must create and Add() to the Game based on a int[] in the DTO (each integer represents the ID of a Platform). And, naturally, EF4 is choking in part because it’s a many-to-many relationship, but also, I think, because there’s some shenanigans going on with how many ObjectContext objects are in play. I keep getting an exception claiming I can’t add my retrieved Platform entities to my new Game entity because they belong to two different ObjectContexts. I can’t see how that is possible given my current set up, but I’m not sure what else the problem could be.

Okay, so I have three repositories which I inject into my controller via Ninject interface injection. I create the ObjectContexts in each like so:

public class HGGameRepository : IGameRepository
{
    private HGEntities _siteDB = new HGEntities();

    // rest of repo
}

The other two repositories are built the same way.

My Ninject DI code is fairly simple:

private class HandiGamerServices : NinjectModule
{
    public override void Load()
    {
        Bind<IArticleRepository>().To<HGArticleRepository>().InRequestScope();
        Bind<IGameRepository>().To<HGGameRepository>().InRequestScope();
        Bind<INewsRepository>().To<HGNewsRepository>().InRequestScope();
        Bind<ErrorController>().ToSelf().InRequestScope();
    }
}

From what I’ve read, this should create these bindings once per HTTP request.

What I’d like to do is have one instance of my HGEntities object be shared among all repositories in order to ensure I have one and only one ObjectContext in play. I’m just unsure how to do it.

Is there a standard way to do 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-22T21:36:49+00:00Added an answer on May 22, 2026 at 9:36 pm

    Here’s one option:

    Change your repositories to take in an interface, IHGEntities, in their constructor and hook up HGEntities into your NinjectModule the same you did with your repositories. That way, when your controllers need an instance of IArticleRepository, Ninject will either instantiate an instance of HGEntities to pass into the repositories or use the instance that is already active in the current HTTP context.

    Then, inside your repository classes you can simply cast IHGEntities to HGEntities.

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

Sidebar

Related Questions

This is a continuation question from a previous question I have asked I now
This is a continuation of my question about reading the superblock . Let's say
This is in continuation with the question posted here: Finding the center of mass
This is a continuation of questions like this one . Are there any guidelines
This could be considered a continuation of this earlier SO question . Ideally, I'd
This is a continuation of the post How does one access a method from
This is a continuation from When I update/insert a single row should it lock
This question is in continuation to my previous post located here . Since there
In continuation of the question at this link I wanted to add some functionality
A continuation from this question I need a SQL statement that returns the number

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.