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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:33:23+00:00 2026-06-02T18:33:23+00:00

Using Entity Framework Code First, I have something like: public class Foo { public

  • 0

Using Entity Framework Code First, I have something like:

public class Foo
{
    public int Id { get; set; }

    public List<Bar> Bars { get; set; }
}    

Foo foo = (from f in ctx.Foos.Include("Bars") where f.Id == 42 select f).Single();

// At this point foo.Bars is populated

ctx.Entry(foo).State = EntityState.Detached;

// At this point foo.Bars is an empty List

Why does detaching an object cause it’s property public List<string> Bars, which was explicitly and successfully included, to be emptied?

What is the correct procedure to detach an object that may have many properties?

  • 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-02T18:33:25+00:00Added an answer on June 2, 2026 at 6:33 pm

    The reason why the list is emptied is a combination of two rules in Entity Framework:

    1. When you detach an object only this object itself is detached without the objects any navigation properties refer to.

    2. The ObjectContext/DbContext does not allow to hold an object graph which is partially attached to the context and partially detached. Although this can happen as a temporary state when using POCOs EF will always fix this temporary state by automatically attaching detached objects in the graph inside of various methods (like Add, Attach, setting the state of an entity, etc.) or latest when SaveChanges is called.

    This means that when you detach the root object from the context, EF will clear the list of children because: a) the children stay attached (rule 1) and b) a mix of detached and attached objects in the graph is not allowed (rule 2).

    As far as I can tell there is no way to detach an object graph from the context while maintaining the original tree structure. You can detach the parent and then the children one after each other. As a result you have detached all objects of the tree from the context but the tree is destroyed at the same time – each navigation property is nullified.

    The main purpose of detaching entities manually is to release them for garbage collection in situations where you have memory resource limitations and don’t want and need to hold a huge amount of objects in the context. For this purpose it doesn’t matter that the graph structure is destroyed.

    I don’t know why you need to detach objects from the context. But keep in mind that there is also the option to load entities from the database without attaching them to the context in the first place, like using AsNoTracking().

    Another answer about the problem with some references to MSDN documentation is here: https://stackoverflow.com/a/7693732/270591

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

Sidebar

Related Questions

I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I have been using Entity Framework CTP with Code-First as in this tutorial by
I have a strange problem when using Entity Framework code first. When I return
I have a small MVC 3 app using Entity Framework Code First and use
I am currently using Entity Framework 4.2 with Code First. I currently have a
Using the Entity Framework 4. I have created a Code First database in the
I'm using Entity Framework 4.1 Code First. In my entity, I have three date/time
I'm using Entity Framework 4.1 Code First and have been trying to add a
I'm using Entity Framework Code First. Usually I have no problem, but for a
I am using Entity Framework 4.1 code first . Here is my Category class:

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.