2

I have two tables on two different servers. They're both exactly the same, other than the table on one server has no data.

I want to write a LinqToSql program that will copy the data from one table to the other. There's only about 7k records. This is a run-once program.

The error I'm getting is "Ambiguous constructor reference". It's because when I'm trying to create a new Circuit object to write to the sandbox Circuits table, it doesn't know which one I'm talking about: The Circuit in the Dev context or the Circuit in the Sandbox context.

I guess what I need to do know is if I can use an alias, or something, in order to differentiate between the two Circuits data objects in each context?

Basically, I just need to get all the data in one table to an exact duplicate table on another server.

class Program
{
    static void Main(string[] args)
    {
        // set up data contexts
        var circuitsDev = new CircuitsDevDataContext();
        var circuitsSandbox = new CircuitsSandboxDataContext();

        // loop through each record in CircuitsDev data and write it to second table.
        foreach (var circuitData in circuitsDev.Circuits)
        {
            // create new sandbox circuit object
            // This is where error is.
            var circuit = new Circuit(); // <== ambiguous error 

            circuit = circuitData;

            circuitsSandbox.Circuits.InsertOnSubmit(circuit);
        }
        circuitsSandbox.SubmitChanges();
    }
}
Kevin
  • 4,798
  • 19
  • 73
  • 120
  • I think this tool not for jobs like this. Anyway, use fully qualified names. – Hamlet Hakobyan Jan 05 '15 at 23:31
  • Are you familiar with `BULKCOPY` why not do it that way.. || http://stackoverflow.com/questions/11868509/copying-a-row-from-one-table-to-another-using-linq-and-c-sharp – MethodMan Jan 05 '15 at 23:39
  • @DJKRAZE Not familiar at all with SqlBulkCopy. I had considered using that, but since is just a small amount of data, I thought it would be faster to just write a small program vs trying to figure out how to use SqlBulkCopy. But, I may end up using it if I can't find a reasonable way to accomplish what I'm trying to do with the small program above. Thanks! – Kevin Jan 05 '15 at 23:45

2 Answers2

1

Both Circuit classes would be in different namespaces, we can only guess what they are but you would fully qualify the name with the namespace prefixed:

var circuit = new SomeNamespace2.Circuit();

if declared:

namespace SomeNamespace2
{
    public class Circuit {...

The issue you will have, since you have two Circuit classes, is you cannot assign one to the other without a conversion.

circuit = circuitData;// unable to convert SomeNamespace1.Circuit to SomeNamespace2.Circuit

Usually instead you might do it property by property or use something like AutoMapper:

circuit.Name = circuitData.Name;
circuit.Location = circuitData.Location;
//...
AaronLS
  • 37,329
  • 20
  • 143
  • 202
  • This worked for me. I used the namespace for the Sandbox Circuit: `var circuit = new CircuitRebuild2.Circuit();` and the error went away. – Kevin Jan 05 '15 at 23:55
1

change

var circuit = new Circuit();

to

var circuit = new circuitsSandbox.Circuit(); // basically use the full name of the class

and then map one property at a time

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Punit Vora
  • 5,052
  • 4
  • 35
  • 44