I want to do a potentially very large insert on my database, based on other data from my database. Performance matters on this application, and even using a SqlBulkCopy slows this process down a bit too much for my liking.
So the question: can one use Linq to EF to automatically generate and run SQL code on the database, never returning anything back to the user? It would seem possible to me, I just have no idea how to get around it.
Here's the premise: could something like the following ever work?
myContext.OutputTable.AddRange(
myContext.TableA.Join(
myContext.TableB,
a => a.myAKey,
b => b.myBKey,
(a, b) => new { a.fieldA, a.fieldB, b.fieldC }).Select(
output => new OutputTable
{
myOutputColumnA = output.FieldA,
myOutputColumnB = output.FieldB,
myOutputColumnC = output.FieldC
})
);
Maybe what I'm trying to do here isn't obvious. I'm basically trying to insert data into "OutputTable" using data from both TableA and TableB, without letting EntityFramework return the data to the application. Now, I know that it's possible to use ExecuteNonQuery() to run this kind of insert statement, but ultimately, I don't want to do that for a couple of different reasons: I want to hopefully maintain the entirety of the code in C#; I also want to keep the debugging aspect that Linq offers (since I use Visual Studio, it helps to have a query fail in the same place that code fails).
I know that Linq to EF generates SQL that executes on the database, so it would seem possible to me to accomplish this (would possibly have to purposefully ignore lazy loading, so the code actually executes). The code above does not execute, I believe on the premise that a new object cannot be instantiated inside of a Select() statement in that manner (Linq to EF has no idea how to deal with it). So ultimately, is this a feasible path, and if not, are there any viable alternatives?