I am trying to insert multiple records into a table where each record has a sequence number that relies on the sequence number of a previously inserted record.
For example:
var db = new StoreDataContext();
var inserts = payments.Select(p => new payment
{
order_fk = p.order_fk, line_no = DbMethods.GetNextLine(p.order_fk),
amount = p.amount
});
db.payments.InsertAllOnSubmit(inserts);
db.SubmitChanges(ConflictMode.FailOnFirstConflict);
DbMethods.GetNextLine is a SQL scalar function that goes and gets the MAX(line_no) + 1 for that particular order.
My problem is that the function gets performed before the actual inserts happen, so all of the values for line_no are the same. Is there anyway of doing this without submitting each record seperately?
The only way I know to do something like this with Linq to SQL is to wrap it in a transaction.