Entity Framework Extensions Delete Matched and one NOT Condition

Description

The DeleteMatchedAndOneNotCondition option lets you perform or skip the delete action, depending on if at least one value from the source is different than the destination for properties specified.

Example

// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;

context.BulkDelete(customers, options => 
{
	// ON DELETE, remove customer where the version is not equal
	options.DeleteMatchedAndOneNotConditionExpression = x => new { x.Version };
});

Scenario

A company uses Entity Framework and needs to delete customers with the BulkDelete method.

However, there is a particularity. The delete should only happen if the version in the database is not equal to the one coming from the importation.

In summary:

  • If the Version value is equal, the customer cannot be deleted
  • If the Version value is not equal, the customer can be deleted

Solution

The DeleteMatchedAndOneNotCondition option has 4 solutions to this problem:

DeleteMatchedAndOneNotConditionExpression

Use this option if you prefer to specify with an expression which properties you want to include.

// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;

context.BulkDelete(customers, options => 
{
	// ON DELETE, remove customer where the version is not equal
	options.DeleteMatchedAndOneNotConditionExpression = x => new { x.Version };
});
Method Name Try it
BulkDelete DeleteMatchedAndOneNotConditionExpression Fiddle

DeleteMatchedAndOneNotConditionNames

Use this option if you prefer to specify a list of property names you want to include. The value must correspond to the property name or the navigation name.

// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;

context.BulkDelete(customers, options => 
{
	// ON DELETE, remove customer where the version is not equal
	options.DeleteMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.Version) };
});
Method Name Try it
BulkDelete DeleteMatchedAndOneNotConditionNames Fiddle

IgnoreOnDeleteMatchedAndOneNotConditionExpression

Use this option if you prefer to specify with an expression which properties you want to exclude/ignore. All non-specified properties will be included.

// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;

context.BulkDelete(customers, options => 
{
	// ON DELETE, remove customer where the version is not equal (by excluding other properties)
	options.IgnoreOnDeleteMatchedAndOneNotConditionExpression  = x => new { x.CustomerID, x.Name, x.Email, x.Note };
});
Method Name Try it
BulkDelete IgnoreOnDeleteMatchedAndOneNotConditionExpression Fiddle

IgnoreOnDeleteMatchedAndOneNotConditionNames

Use this option if you prefer to specify a list of property names you want to exclude/ignore. The value must correspond to the property name or the navigation name. All non-specified properties will be included.

// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;

context.BulkDelete(customers, options => 
{
	// ON DELETE, remove customer where the version is not equal (by excluding other properties)
	options.IgnoreOnDeleteMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.CustomerID), nameof(Customer.Name), nameof(Customer.Email), nameof(Customer.Note) };
});
Method Name Try it
BulkDelete IgnoreOnDeleteMatchedAndOneNotConditionNames Fiddle

Last updated: 2025-08-13
Author:


Contents