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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:35:33+00:00 2026-06-03T12:35:33+00:00

I’ve been dealing with this issue for a while, and still can’t seem to

  • 0

I’ve been dealing with this issue for a while, and still can’t seem to find a solution. I have several repositories which wrap an EF 4 ObjectContext. An example is below:

public class HGGameRepository : IGameRepository, IDisposable
{
    private readonly HGEntities _context;

    public HGGameRepository(HGEntities context)
    {
        this._context = context;
    }

    // methods

    public void SaveGame(Game game)
    {
        if (game.GameID > 0)
        {
            _context.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Modified);
        }
        else
        {
            _context.Games.AddObject(game);
        }

        _context.SaveChanges();
    }

    public void Dispose()
    {
        if (this._context != null)
        {
            this._context.Dispose();
        }
    }
}

And I have the following NinjectModule:

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

Since I’m using the MVC 2 extension, these bindings default to InRequestScope().

My problem is that the ObjectContext isn’t being handled properly. I get what’s described here: https://stackoverflow.com/a/5275849/399584 Specifically, I get an InvalidOperationException that states:

The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects.

This happens every time I try to update an Entity.

If I set my repos to bind InSingletonScope() it works, but seems like a bad idea.

What am I doing wrong?

EDIT: For clarity, I have just one ObjectContext that I want to share with all my repos per request.

  • 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-03T12:35:34+00:00Added an answer on June 3, 2026 at 12:35 pm

    You have to specify InRequestScope() in your module. Based on this article the default to transient, which is why you are getting more than one context.

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

    Also did you add ninject to your project via nuget package manager or the old fashion way?

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.