Dapper Plus - Bulk Delete

Description

DELETE entities using Bulk Operation.

Example - Delete Single

DELETE a single entity with Bulk Operation.

  1. DapperPlusManager.Entity<Customer>().Table("Customers").Key("CustomerID");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.BulkDelete(connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList());
  5. }

Try it: .NET Core | .NET Framework

Example - Delete Many

DELETE many entities with Bulk Operation.

  1. DapperPlusManager.Entity<Customer>().Table("Customers").Key("CustomerID");
  2. using (var connection = new SqlConnection(FiddleHelper.GetConnectionStringSqlServerW3Schools()))
  3. {
  4. connection.BulkDelete(connection.Query<Customer>("Select * FROM CUSTOMERS WHERE CustomerID in (53,57) ").ToList());
  5. }

Try it: .NET Core | .NET Framework

Example - Delete with relation (One to One)

DELETE 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.BulkDelete(suppliers.Select(x => x.Product)).BulkDelete(suppliers);
  6. }

Try it: .NET Core | .NET Framework

Example - Delete with relation (One to Many)

DELETE 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.BulkDelete(suppliers.SelectMany(x => x.Products)).BulkDelete(suppliers);
  6. }

Try it: .NET Core | .NET Framework