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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:29:58+00:00 2026-06-07T13:29:58+00:00

I have two methods using the EF4 – one which returns a list of

  • 0

I have two methods using the EF4 – one which returns a list of objects (Pages) within a using block, and another which creates a different type of object (Book). One of the properties on Book is FirstPage, which is an item from the first list of items. When I try to set the FirstPage property on this instantiated object, I get an exception:

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

I guess this must be because the using block has disposed before the object was detached. Is there any way to either 1) detach it after the context has been disposed or 2) atach it to a ghost context until I pass it back to the data layer?

This is my code:

IEnumerable<Page> allPages = null;

using (var db = new DataContainer())
{
    var items = db.Pages;
    var filteredCode = items.Where(i => i.Code == PageCode);
    allPages = filteredCode.ToList();
}

Page page = allPages.FirstOrDefault(p => ...); // query omitted

var book = new Book()
{
    Title = @"asdas",
    FirstPage = page, // 'page' is never null
    // rest omitted
};
  • 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-07T13:29:59+00:00Added an answer on June 7, 2026 at 1:29 pm

    Answer to both your questions is: no. Either use the same context for both operations or detach your entities prior to leaving scope of the using block.

    If you are using POCOs you can also turn off proxy creation (=no lazy loading and dynamic change tracking) and your code will work without any changes because the proxy keeps reference to the context – POCO itself doesn’t know anything about context.

    Btw. there is another side effect of not detaching entity (Proxied POCO and probably also EntityObject) before it goes out of lifetime scope of its context – it causes memory leak because the entity holds reference to the context and context holds references to many other objects including other attached entities. Until your entity is released (there is no reference to your entity) the garbage collector cannot release the context and all related objects.

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

Sidebar

Related Questions

I have two methods, one called straight after another, which both throw the exact
I have two methods, one which counts the number of objects which are considered
I have two methods; one of which resides outside of a class, the other
I have two methods from which first i am using to insert data and
I have two methods, one that I use to convert an image to a
Can somebody explain me one thing. I have two methods in my controller :
I have two helper methods inside a custom View which are called by their
I have two methods on the page. One AddMessageToInboxOutbox(params[]) and other one SendNewMessageMail(params[]). Once
I have two methods in a java class that both have a block of
I have two queries in my RIA DomainService. one is a simple get using

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.