I have a C# data structure like so:
public class Invoice
{
public string InvoiceNumber {get;set;}
public List<SalesPerson> SalesPersons {get;set;}
}
public class SalesPerson
{
public string Name {get;set;}
}
This corresponds to two tables: Invoice and SalesPerson. An invoice can have any number of these associations.
Now, I'm creating a bulk upload facility, taking in a CSV and creating a collection of Invoices (which each having some number of associated SalesPerson records).
I can think of a naive way to do this with Dapper: loop through each Invoice, insert it, get identity (an integer ID column), loop through each SalesPerson in that invoice, insert using the identity I got as a foreign key. But that seems like it's going to be a lot of unnecessary traffic to/from my SQL server.
I'm aware of the 'bulk' insert where I can execute an insert command multiple times, but I don't see how that would work in this case, since I don't know the foreign key at the time I'm constructing the parameters.
Is there a better way to do this insert than the naive way I've outlined above? Is Dapper the wrong tool for this? (I'm using SQL Server; if there's some tSQL command(s) that can help me, I'm unaware of them)