using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Data.SqlClient;
public static void Main()
DapperPlusManager.Entity<Customer>("KeepIdentity")
.UseBulkOptions(options => {
options.InsertKeepIdentity = true;
options.MergeKeepIdentity = true;
.Identity(x => x.CustomerID, true);
DapperPlusManager.Entity<Order>()
.Identity(x => x.OrderID, true);
DapperPlusManager.Entity<OrderItem>()
.Identity(x => x.OrderItemID, true);
DapperPlusManager.Entity<Invoice>()
.Identity(x => x.InvoiceID, true);
DapperPlusManager.Entity<InvoiceItem>()
.Identity(x => x.InvoiceItemID, true);
var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
connection.CreateTable<Customer>();
connection.CreateTable<Order>();
connection.CreateTable<OrderItem>();
connection.CreateTable<Invoice>();
connection.CreateTable<InvoiceItem>();
connection.SingleInsert("KeepIdentity", new Customer() { CustomerID = 13, Name = "Jonathan Magnan" });
var newCustomerOrders = GetNewCustomerOrders();
connection.BulkMerge("KeepIdentity", newCustomerOrders)
.Include(c => c.ThenBulkInsert(customer => customer.Orders)
.AlsoBulkInsert(order => order.Items))
.Include(c => c.ThenBulkInsert(customer => customer.Invoices)
.AlsoBulkInsert(invoice => invoice.Items));
FiddleHelper.WriteTable("1 - Customers", connection.Query<Customer>("SELECT * FROM [Customer]"));
FiddleHelper.WriteTable("2 - Orders", connection.Query<Order>("SELECT * FROM [Order]"));
FiddleHelper.WriteTable("3 - OrderItems", connection.Query<OrderItem>("SELECT * FROM [OrderItem]"));
FiddleHelper.WriteTable("4 - Invoice", connection.Query<Invoice>("SELECT * FROM [Invoice]"));
FiddleHelper.WriteTable("5 - InvoiceItems", connection.Query<InvoiceItem>("SELECT * FROM [InvoiceItem]"));
public static List<Customer> GetNewCustomerOrders()
var customers = new List<Customer>();
customers.Add(new Customer() { CustomerID = 13, Name = "Jonathan Magnan", Orders = new List<Order>() {
new Order() { State = "New", Items = new List<OrderItem>() {
new OrderItem() { ProductName = "Dapper Plus", Quantity = 9 },
new OrderItem() { ProductName = "C# Eval Expression", Quantity = 14 }
Invoices = new List<Invoice>() {
new Invoice() { TotalPrice = 2198, Items = new List<InvoiceItem>() {
new InvoiceItem() { UnitPrice = 155.44m, Quantity = 9 },
new InvoiceItem() { UnitPrice = 47.07m, Quantity = 14 }
customers.Add(new Customer() { CustomerID = 33, Name = "ZZZ Projects", Orders = new List<Order>() {
new Order() { State = "New", Items = new List<OrderItem>() {
new OrderItem() { ProductName = "Entity Framework Extensions", Quantity = 19 }
Invoices = new List<Invoice>() {
new Invoice() { TotalPrice = 1799, Items = new List<InvoiceItem>() {
new InvoiceItem() { UnitPrice = 94.68m, Quantity = 19 }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CustomerID { get; set; }
public string Name { get; set; }
public List<Order> Orders { get; set; }
public List<Invoice> Invoices { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int OrderID { get; set; }
public int CustomerID { get; set; }
public string State { get; set; }
public List<OrderItem> Items { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int OrderItemID { get; set; }
public int OrderID { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InvoiceID { get; set; }
public int CustomerID { get; set; }
public decimal TotalPrice { get; set; }
public List<InvoiceItem> Items { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int InvoiceItemID { get; set; }
public int InvoiceID { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }