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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:41:12+00:00 2026-05-27T18:41:12+00:00

Having 2 separate databases with the same schema, I need to copy entities (records)

  • 0

Having 2 separate databases with the same schema, I need to copy entities (records) from one database to another using Entity Framework 4.

I’m creating 2 Contexts, but I’m getting the following error when I add one entity to the second Context:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

I know I can avoid that error if i use the Detach method, but in that case the related entities are lost!

Sample code:

        var cx = new MyEntities();

        //eager load related tables
        var allEntities = from x in cx.Reservation.Include("Detail.MoreDetail")
                  select x;

        // new instance of context but connected to a second database
        var cx2 = new MyEntities( new ConnectionString...);
        foreach (var e in allEntities)
        {
            //cx.Detach(reservation);  // can't detach, or related entities will be lost
            cx2.AddToReservation(reservation);  // error happens here!
            cx2.SaveChanges();                
        }

How can I perform such operation?
Alternatively, how can I detach the entity without losing the related entities?

  • 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-27T18:41:13+00:00Added an answer on May 27, 2026 at 6:41 pm

    For once the error message is helpful – entities can only belong to one context at a time. To do what you’re wanting you’ll need to Detatch each entity from the first context before adding it to the second.

    Like you said, this will kill related entities. Unfortunately you’ll have to deal with this (annoying) aspect of Detach.

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

Sidebar

Related Questions

I'm having an issue calling a method from a separate project within the same
Having in mind that each webapp has its own separate database (but all databases
I suspect that calls from separate threads (>15) are having a negative effect on
I have an odd need to open two separate instances of excel and having
Having to upgrade a database schema makes installing a new release of software a
I am designing a database and initially I setup addresses separate from orders so
I am currently stuck, having two separate glossaries: main & acronyms . Acronyms glossary
I like the idea of having Interfaces and Implementation separate. But how separate? Are
Having a heckuva time with this one, though I feel I'm missing something obvious.
Unfortunately, the term schema has come to take on different definitions for different databases.

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.