Dapper Plus - Bulk Merge

Description

MERGE entities using Bulk Operation.

Example - Merge Single

MERGE a single entity with Bulk Operation.

  1. DapperPlusManager.Entity<Customer>().Table("Customers");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.BulkMerge(new List<Customer>() { new Customer() { CustomerName = "ExampleBulkMerge", ContactName = "Example Name :" + 1}});
  5. }

Try it: .NET Core | .NET Framework

Example - Merge Many

MERGE many entities with Bulk Operation.

  1. DapperPlusManager.Entity<Customer>().Table("Customers");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.BulkMerge(customers);
  5. }

Try it: .NET Core | .NET Framework

Example - Merge with relation (One to One)

MERGE entities with a one to one relation with Bulk Operation.

  1. DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
  2. DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
  3. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  4. {
  5. connection.BulkMerge(suppliers).ThenForEach(x => x.Product.SupplierID = x.SupplierID).ThenBulkMerge(x => x.Product);
  6. }

Try it: .NET Core | .NET Framework

Example - Merge with relation (One to Many)

MERGE entities with a one to many relations with Bulk Operation.

  1. DapperPlusManager.Entity<Supplier>().Table("Suppliers").Identity(x => x.SupplierID);
  2. DapperPlusManager.Entity<Product>().Table("Products").Identity(x => x.ProductID);
  3. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  4. {
  5. connection.BulkMerge(suppliers).ThenForEach(x => x.Products.ForEach(y => y.SupplierID = x.SupplierID)).ThenBulkMerge(x => x.Products);
  6. }

Try it: .NET Core | .NET Framework