πŸ—‘οΈβ“ Delete Matched and One NOT Condition Option in Entity Framework ExtensionsDelete only when at least one value is different

The DeleteMatchedAndOneNotCondition option in Entity Framework Extensions lets you perform the DELETE action only when at least one selected value in the source and destination is different.

If all selected values are equal, the delete is skipped. This is useful when you want to protect certain records, enforce rules, or avoid removing entries that are still valid.

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

context.BulkDelete(customers, options => 
{
    // ON DELETE: only remove if "Version" is different between source and destination
    options.DeleteMatchedAndOneNotConditionExpression = x => new { x.Version };
});

Typical scenarios include:

  • Versioning (Version) β†’ Only delete if the record version differs, preventing accidental removal of up-to-date records.
  • Approval checks (ApprovedBy, ApprovedDate) β†’ Protect approved records from deletion unless one of these values changes.
  • Synchronization rules (SyncKey, LastSyncDate) β†’ Delete only when a mismatch indicates outdated or invalid records.
  • User profiles (Email, Role) β†’ Avoid deleting active users unless at least one critical field differs.

This option applies to the following methods in Entity Framework Extensions:


πŸ’‘ Example Effect

ID Destination.Version Source.Version Without Condition With Condition
1 1 1 deleted skipped
2 3 4 deleted deleted
3 5 5 deleted skipped

πŸ› οΈ Prerequisites

Before continuing, we recommend reading these articles first to understand how EF Extensions options work and the differences between column option types:

  • Configure Options – Learn the basics of setting and customizing options in EF Extensions.
  • Configure Column Options – See how to configure column-specific behavior and when to use Expression (strongly typed, compile-time safe) vs. Names (string-based, dynamic at runtime).

πŸ“Œ When to Use

Use the DeleteMatchedAndOneNotCondition option from Entity Framework Extensions when:

  • You want to delete only if at least one key field is different
  • You need to protect records from being deleted if nothing meaningful has changed
  • You want to enforce rules on deletions during synchronization or imports

⭐ Why It’s Useful

Without DeleteMatchedAndOneNotCondition, using in EF Core a BulkDelete from Entity Framework Extensions could remove rows unnecessarily, even when the values are still valid.

Using this option, you can:

  • Prevent unwanted deletions
  • Protect important or approved records
  • Keep synchronization logic simpler without custom SQL

🏒 Scenario

A company imports in EF Core customers using BulkDelete method from Entity Framework Extensions.

  • Records should only be deleted if the Version field is different between source and destination.
  • If versions match, the row should stay untouched.

In summary:

  • If Version differs β†’ delete the customer
  • If Version is the same β†’ skip deletion

πŸ—οΈ Solution

The DeleteMatchedAndOneNotCondition option offers four ways to configure behavior:


🏷️ DeleteMatchedAndOneNotConditionExpression

Use this option to specify β€” with a lambda expression β€” which property values should be compared. If at least one property value is different, the delete is executed.

context.BulkDelete(customers, options => 
{
    options.DeleteMatchedAndOneNotConditionExpression = x => new { x.Version };
});
Method Option Name Try it
BulkDelete DeleteMatchedAndOneNotConditionExpression Online Example

🏷️ DeleteMatchedAndOneNotConditionNames

Use this option to specify β€” with a list of property names β€” which property values should be compared. If at least one property value is different, the delete is executed.

context.BulkDelete(customers, options => 
{
    options.DeleteMatchedAndOneNotConditionNames = new List<string>() 
    { 
        nameof(Customer.Version) 
    };
});
Method Option Name Try it
BulkDelete DeleteMatchedAndOneNotConditionNames Online Example

🏷️ IgnoreOnDeleteMatchedAndOneNotConditionExpression

Use this option to specify β€” with a lambda expression β€” which property values should be excluded from comparison. All other property values will be checked, and if at least one is different, the delete is executed.

context.BulkDelete(customers, options => 
{
    options.IgnoreOnDeleteMatchedAndOneNotConditionExpression = x => new { x.CustomerID, x.Name, x.Email, x.Note };
});
Method Option Name Try it
BulkDelete IgnoreOnDeleteMatchedAndOneNotConditionExpression Online Example

🏷️ IgnoreOnDeleteMatchedAndOneNotConditionNames

Use this option to specify β€” with a list of property names β€” which property values should be excluded from comparison. All other property values will be checked, and if at least one is different, the delete is executed.

context.BulkDelete(customers, options => 
{
    options.IgnoreOnDeleteMatchedAndOneNotConditionNames = new List<string>() 
    { 
        nameof(Customer.CustomerID), nameof(Customer.Name), nameof(Customer.Email), nameof(Customer.Note) 
    };
});
Method Option Name Try it
BulkDelete IgnoreOnDeleteMatchedAndOneNotConditionNames Online Example

🏁 Conclusion

Using DeleteMatchedAndOneNotCondition option in Entity Framework Extensions with EF Core ensures that deletions only happen when at least one important value is different between source and destination.

You can choose:

  • Expression-based include β†’ DeleteMatchedAndOneNotConditionExpression
  • Name-based include β†’ DeleteMatchedAndOneNotConditionNames
  • Expression-based exclude β†’ IgnoreOnDeleteMatchedAndOneNotConditionExpression
  • Name-based exclude β†’ IgnoreOnDeleteMatchedAndOneNotConditionNames

By using DeleteMatchedAndOneNotCondition in your bulk operations, you can:

  • Skip unnecessary deletions when values match
  • Protect sensitive or important records
  • Keep synchronization safer and more predictable

Column Options

Coalesce Options

Matched Options

Delete Matched Options


Last updated: 2025-08-17
Author: