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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:19:06+00:00 2026-05-14T23:19:06+00:00

I am a pretty much a newbie to Entity Framework (specifically version 4) and

  • 0

I am a pretty much a newbie to Entity Framework (specifically version 4) and I am trying to understand an issue I have.

I have an ASP.NET MVC2 project I am using with Entity Framework 4 and a Repository class. I have a simple foreign key relationship that I have defined. When I run my MVC project I am able to load the MVC form and update the foreign key relationship by setting Foreign Key ID on my entity. I do this by calling the OnPropertyIDChanging in a partial class and setting the EntityKey like so

this.ManufacturerReference.EntityKey = new System.Data.EntityKey("MachinesEntities.Manufacturers", "ManufacturerId", value);

I have created a second project which is a command line project I use to load the initial data for the model. However, when I call into the Repository object to add the entity I get a cannot insert Null values into the child entity table (Manufacturer). If I explicitly set a reference on the parent object (Product) to the child object (Manufacturer) calling my repository to get a manufacturer object and setting the product’s manufacturer object I get an exception stating

An entity object cannot be referenced
by multiple instances of
IEntityChangeTracker

I assume this is a result of having a global reference to the Entity context on each of my repository objects, and as a result I have an open context for the Manufacturer and the product.

In doing some research, I have seen that by loading the object from a query, that the object relationships are explicitly created via a RelationshipEntry object. This does not happen when I am creating the objects in my command line application. What is the way to get around this? I assume I will have the same issue if I try and create a Web Service since I am creating the object without calling SQL first.

Secondly, is there a best practice on managing the context? Is it better to have the context created and disposed within each repository method, a global reference in each of the repository objects or a context that gets passed to each repository constructor?

Thanks!

John

  • 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-14T23:19:07+00:00Added an answer on May 14, 2026 at 11:19 pm

    Secondly, is there a best practice on managing the context? Is it better to have the context created and disposed within each repository method, a global reference in each of the repository objects or a context that gets passed to each repository constructor?

    Actually, this is the important problem. Fix this and the other one should go away.

    The context should be a unit of work. So for your web app, that will be one request. There should be exactly one context per unit of work. Use DI to share it amongst all code which participates in that unit of work, probably via your repositories.

    For your command-line app, decide what the unit of work is.

    This message:

    An entity object cannot be referenced by multiple instances of IEntityChangeTracker

    …just means, “You can’t relate two entities which belong to different contexts. The real issue here is having two different contexts alive concurrently at all. This is not impossible, but so tricky to get right I consider it an “experts-only” technique.

    This hack:

    this.ManufacturerReference.EntityKey = new System.Data.EntityKey("MachinesEntities.Manufacturers", "ManufacturerId", value);
    

    …isn’t necessary in EF 4 anymore. In EF 4 you can generate your model with FK associations, and then just do:

    this.ManufacturerId = value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Pretty much I have a code like below that I'm trying to add </tr><tr>
I'm a total newbie when it comes to CSS so I've pretty much given
I'm pretty much a complete newbie to PHP. My background is C/C++ and C#.
Im pretty much new to data mining and recommendation systems, now trying to build
I am pretty much a newbie to objective-c and as I started to program,
I'm pretty much a newbie and I need to dig into this matter to
Pretty much a complete Linux/Apache newbie here, I am in the process of moving
I have done pretty much all my programming using C# and very much a
Pretty much what I am trying to do, is create the following image: So
I'm pretty much a newbie with opengl. I imagine glReadPixels is horrible so whats

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.