ποΈβ 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 β include properties via a lambda expression
- DeleteMatchedAndOneNotConditionNames β include properties via a list of property names
- IgnoreOnDeleteMatchedAndOneNotConditionExpression β exclude properties via a lambda expression, all others properties are included.
- IgnoreOnDeleteMatchedAndOneNotConditionNames β exclude properties via a list of property names, all others properties are included.
π·οΈ 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
π Related Articles
Column Options
Coalesce Options
Matched Options
Delete Matched Options
ZZZ Projects