I'm trying to copy data from table A to table B, and after it's inserted update FK column in table A to newly inserted table B Id. Because theese needs to be related.
INSERT INTO TableB(ColA, ColB, ColC, ColD, ...)
SELECT a.ColA, a.ColB, a.ColC, a.ColD, ...
FROM TableA a for update;
update a set TableBId = (newly inserted id from table B)
In code for better understanding
foreach (var tableA in AllTableA)
{
TableB newB = new TableB(tableA);
AllTableB.Add(newB);
tableA.TableBId = newB.GetId();
}
How can I do this in one transaction?