using System.Collections.Generic;
using System.Data.DataSetExtensions;
using System.Data.SqlClient;
public static void Main()
var invoices = GenerateInvoices(5, 3);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
ShowData(connection, "Before");
using (var bulk = new BulkOperation<Invoice>(connection))
bulk.DestinationTableName = "Invoices";
bulk.AutoMapOutputIdentity = true;
bulk.BulkMerge(invoices);
using (var bulk = new BulkOperation<InvoiceItem>(connection))
invoices.ForEach(x => x.Items.ForEach(y => y.InvoiceID = x.InvoiceID));
bulk.DestinationTableName = "InvoiceItems";
bulk.BulkMerge(invoices.SelectMany(x => x.Items).ToList());
ShowData(connection, "After");
public static void ShowData(SqlConnection connection, string text)
using (var command = connection.CreateCommand())
SELECT * FROM InvoiceItems
var sqlDataAdapter = new SqlDataAdapter(command);
ds.Tables[0].TableName = "1 - Invoices " + text;
ds.Tables[1].TableName = "2 - InvoiceItems " + text;
FiddleHelper.WriteTable(ds.Tables[0]);
FiddleHelper.WriteTable(ds.Tables[1]);
public static List<Invoice> GenerateInvoices(int countInvoice, int countInvoiceItem, bool useOld = false)
List<Invoice> list = new List<Invoice>();
for(int i = 0; i < countInvoice; i++)
var invoice = new Invoice() { InvoiceID = (i + 1), Number = (useOld ? "Old_" : "") + "Number_" + i, Items = new List<InvoiceItem>() };
for(int j = 0; j < countInvoiceItem; j++)
invoice.Items.Add(new InvoiceItem() { InvoiceItemID = (i * countInvoiceItem) + (j + 1), ProductName = (useOld ? "Old_" : "") + "ProductName_" + i + "_" + j });
public static void SeedInvoices(int countInvoice, int countInvoiceItem)
var invoices = GenerateInvoices(countInvoice, countInvoiceItem, true);
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
using (var bulk = new BulkOperation<Invoice>(connection))
bulk.DestinationTableName = "Invoices";
bulk.ColumnOutputExpression = invoice => invoice.InvoiceID;
bulk.ColumnInputExpression = invoice => new
bulk.BulkInsert(invoices);
using (var bulk = new BulkOperation<InvoiceItem>(connection))
invoices.ForEach(x => x.Items.ForEach(y => y.InvoiceID = x.InvoiceID));
bulk.DestinationTableName = "InvoiceItems";
bulk.BulkInsert(invoices.SelectMany(x => x.Items).ToList());
public static void GenerateDatabase()
using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()))
using (var command = connection.CreateCommand())
[InvoiceID] [INT] IDENTITY(1,1) NOT NULL,
[Number] [NVARCHAR](MAX) NULL,
CONSTRAINT [PK_Invoices] PRIMARY KEY CLUSTERED
CREATE TABLE [InvoiceItems]
[InvoiceItemID] [INT] IDENTITY(1,1) NOT NULL,
[ProductName] [NVARCHAR](MAX) NULL,
CONSTRAINT [PK_InvoiceItems] PRIMARY KEY CLUSTERED
ALTER TABLE [InvoiceItems] WITH CHECK ADD CONSTRAINT [FK_InvoiceItems_InvoiceID] FOREIGN KEY([InvoiceID])
REFERENCES [Invoices] ([InvoiceID])
command.ExecuteNonQuery();
public int InvoiceID { get; set; }
public string Number { get; set; }
public List<InvoiceItem> Items { get; set; }
public int InvoiceItemID { get; set; }
public string ProductName { get; set; }
public int InvoiceID { get; set; }