Solved

Simply take a row from one datacontext and write it to an identical database table in another datacontext

Posted on 2009-05-09
4
548 Views
Last Modified: 2013-11-11
This worked until a few days ago on my test system, I don't know what circumstances changed but what am I doing wrong please?

I have two databases on my sql server. One is the master database and holds a single row (with 25 columns) per item stored.  I have a duplicate table on a separate database on the same server that I use like an archive.

In my code behind, I set up two datacontexts, one pointing to the master database table and one (that uses a different connection string on creation) that points to the archive database table. This all works fine and both datacontexts are available and working.

I then read a single row from masterdatacontext into an entity called, say, "masterRec" and then try and do an "insertonsubmit" on the archive datacontext using the whole "masterRec" I've read.  I don't want to have to specify each individual column in the write as this is hideously inefficient and long-winded.

This all worked fine for a week or two, but today it doesn't work. There is an exception about trying to attach an invalid datacontext or something?

All I want to do is read from one datacontext and write the whole row in one go to the archive datacontext. That's not too much to ask is it?
0
Comment
Question by:carled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24346002
1) I don't know LINQ that well to answer your question as is.

2) This question has been open for a while, without response.

3) I like questions that challenge me to find an answer for things that I don't understand.

4) I would like to help you, but it might be easier if you show me what you have tried, and what has failed (exact exceptions).
0
 
LVL 1

Author Comment

by:carled
ID: 24347634
In a nutshell in pseudo-code:

dim altConnString as string = "alternative db connection string"
dim dc1 as db1datacontext = new db1datacontext (default data context)
dim dc2 as db2datacontext = new db1datacontext(altConnString) (same datacontext, different connection string)

dim dc1Rec = (from dc1.table1 where x=y select).firstordefault
if (not dc1Rec is nothing) then
    dc2.table1.insertOnSubmit(dc1Rec)
    dc2.submitChanges()
end if

Like I say, this worked a week or so back as I got data onto the second database, but whether that was a peculiarity of connection strings or something, I don't know, as the above code now generates:
"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."
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 24348140
What does the table structure look like?  Do you have dependent objects?  

I did find a suggestion that you detach from the first DataContext, and then attach to the second.

Linq to SQL: How to Attach object to a different data context
http://weblogs.asp.net/omarzabir/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question