Dapper Plus - Bulk Extensions Methods - Async with CancellationToken

CSharp · Console

/Program.cs

// Dapper Plus
// Doc: https://dapper-plus.net/overview

// @nuget: Dapper
// @nuget: Microsoft.Data.SqlClient
// @nuget: Z.Dapper.Plus

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Dapper;
using Z.Dapper.Plus;

public class Program
{	
	public static async Task Main()
	{
		// Map your entity
		DapperPlusManager.Entity<Customer>("KeepIdentity") // We will learn about mapping key such as "KeepIdentity" later
			.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);
		
		var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer());
		connection.CreateTable<Customer>();
		connection.CreateTable<Order>();
		connection.CreateTable<OrderItem>();
		
		// Seed customer
		connection.SingleInsert("KeepIdentity", new Customer() { CustomerID = 13, Name = "Jonathan Magnan" });
		
		// Get new customers and orders from an external system
		var newCustomerOrders = GetNewCustomerOrders();
		
		// Save new customers and orders
		CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
        CancellationToken token = cancellationTokenSource.Token;
		
		await(await(await connection.UseBulkOptions(options => options.CancellationToken = token).BulkMergeAsync("KeepIdentity", newCustomerOrders))
				  .ThenBulkInsertAsync(customer => customer.Orders))
				  .AlsoBulkInsertAsync(order => order.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]"));
	}
	
	public static List<Customer> GetNewCustomerOrders()
	{
		var customers = new List<Customer>();
		
		// customer #1
		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 }	
			}
		}}});
		
		// customer #2
		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 }	
			}
		}}});
		
		return customers;
	}
	
	[Table("Customer")]
	public class Customer
	{
		[Key]
		[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
		public int CustomerID { get; set; }
		public string Name { get; set; }
		
		public List<Order> Orders { get; set; }
	}
	
	[Table("Order")]
	public class Order
	{
		[Key]
		[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
		public int OrderID { get; set; }
		public int CustomerID { get; set; }
		public string State { get; set; }	
		
		public List<OrderItem> Items { get; set; }
	}
	
	[Table("OrderItem")]
	public class OrderItem
	{
		[Key]
		[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
		public int OrderItemID { get; set; }
		public int OrderID { get; set; }
		public string ProductName { get; set; }
		public int Quantity { get; set; }
	}
}
;