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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:20:50+00:00 2026-06-08T02:20:50+00:00

I tried the following to detach a graph of entity objects, and then attach

  • 0

I tried the following to detach a graph of entity objects, and then attach it to a new context:

// create a context
var ctx = new TestEntities();
var parents = ctx.Parents;

// populate the graph
var newParent = new Parent {Nb = 1, Title = "Parent1"};
parents.AddObject(newParent);
newParent.Children.Add(new Child {Nb = 1, Title = "Child1"});

// put all entity objects in Unchanged state before detaching
ctx.SaveChanges(); 

// detach all entity objects
foreach (var objectStateEntry in ctx.ObjectStateManager.GetObjectStateEntries(~EntityState.Detached))
    ctx.Detach(objectStateEntry.Entity);

// create a new context
ctx = new TestEntities(); 

// attach graphs to new context
foreach (var p in parents)
    ctx.Attach(p);

I have two issues with this approach:

  1. After detaching all entity objects, newParent.Children becomes empty
  2. An InvalidOperationException is raised when re-attaching saying that “An entity object cannot be referenced by multiple instances of IEntityChangeTracker”.

Does anyone know how to properly detach a graph from an ObjectContext, and re-attach it to another one?

UPDATE:

Ok good news for me, I figured out how to change the underlying database connection within the same ObjectContext, so I don’t need to detach/attach anymore. If anybody’s interested, here’s how I do it (here I use SQLite and change the database file):

var sc = ((EntityConnection)ctx.Connection).StoreConnection;
sc.ConnectionString = @"Data Source=" + newFile + ";";

I’ll accept Ladislav’s answer as it seems to be correct and answers my question as it was asked.

  • 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-08T02:20:52+00:00Added an answer on June 8, 2026 at 2:20 am

    You must create deep clone of the whole graph and attach it to another context. The deep clone is created through the serialization. The common approach is to use DataContractSerializer:

    var serializer = new DataContractSerializer(typeof(Parent));
    serializer.WriteObject(stream, attachedEntity);
    stream.Seek(0, SeekOrgin.Begin);
    detachedEntity = (Parent)serializer.ReadObject(stream);
    

    To make this work your entities must not contain circular references (Parent has navigation property to Child and Child has navigation property to Parent) or you must use DataContract(IsReference=true) and DataMember attributes on your entities to inform serializer that it must track references to resolve circular reference issue.

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

Sidebar

Related Questions

I've tried following this tutorial (http://www.raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d) however as you can probably tell I'm not
I've tried following code in ie, firefox and node.js var x = 10; var
I tried following, on form1, Dim removedtabs as New Collection() and on form2 I
I have tried following the documentation and examples of MVC Controls Toolkit to create
I tried following the examples, using a Using statement to open an instance, create
How can I create more than one label variable programmatically I tried following code
I need to detach some entity objects from the database to make them unmanaged.
i am new to push notifications and tried following to fix this but it
I tried following codes for adding new contact which is not working in my
Tried following the instructions here: How to use Google app engine with my own

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.