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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:48:20+00:00 2026-05-17T17:48:20+00:00

I am trying to merge data between two identical schema databases using Linq-to-sql: List<Contact>

  • 0

I am trying to merge data between two identical schema databases using Linq-to-sql:

List<Contact> contacts = (from c in oldDb.Contact
    select c).ToList();
contacts.ForEach(c => c.CreatedByID = 0);
newDb.Contact.InsertAllOnSubmit(contacts);
newDb.SubmitChanges();

Merely throws an “An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.” exception.

Other than doing the following, how else can this be done generically (in reasonable execution time):

List<Contact> contacts = (from c in oldDb.Contact
    select c).ToList();
contacts.ForEach(c => { c.CreatedByID = 0; newDb.Contact.InsertAllOnSubmit(contacts); });
newDb.SubmitChanges();

along with:

private t GetNewObject<t>(t oldObj)
{
    t newObj = (t)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(typeof(t).Name);

    PropertyInfo[] props = typeof(t).GetProperties();
    foreach (PropertyInfo _prop in props)
    {
        _prop.SetValue(newObj, _prop.GetValue(oldObj, null), null);
    }
    return newObj;
}

The problem is this method is rather slow when there’s only 11 objects and 75 properties, I need to do this for a couple hundred thousand objects so any performance gains I can get at this end would greatly reduce overall run time.

Basically, is there any Detach or similar call I could do that will disconnect the existing objects from the old DataContext and connect them to the new DataContext. Without having to create all new objects for each and every one of the returned rows.

  • 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-17T17:48:21+00:00Added an answer on May 17, 2026 at 5:48 pm

    I didnt got the

    contacts.ForEach(c => { c.CreatedByID = 0; newDb.Contact.InsertAllOnSubmit(contacts); });
    

    shouldnt be something like

    contacts.ForEach(c => { 
        Contact c2 = GetNewObject<Contact>(c); 
        c2.CreatedByID = 0; 
        newDb.Contact.InsertOnSubmit(c2); 
    });
    

    also, here’s a way of detaching the object from the old database: http://omaralzabir.com/linq_to_sql__how_to_attach_object_to_a_different_data_context/

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

Sidebar

Related Questions

I'm trying to switch between 2 templates on the basis of a data trigger.
I am trying to generate word documents using the mail merge feature in Microsoft
I'm trying to create an associative array with dynamic data, and having some trouble.
I am trying to use git as something it wasn't made for - a
I'm trying to compile an open-source project . One of the steps involves running
I have a JIRA environment which already has some information and i'm trying to
I'm writing a script to extract some useful data about a series of chemical
I'm trying to automate some of the systems at work here specifically to do
Hi I am trying to build an application which has models resembling something like

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.