πβ Matched and One NOT Condition Option in Entity Framework ExtensionsUpdate only when at least one value is different
The MatchedAndOneNotCondition
option in Entity Framework Extensions lets you perform the UPDATE
action only when at least one selected value in the source and destination is different.
If all selected values are equal, the update is skipped. This gives you more precise control over when updates should happen β for example, to ignore insignificant changes, update only important fields, or optimize imports by avoiding unnecessary updates.
// @nuget: Z.EntityFramework.Extensions.EFCore using Z.EntityFramework.Extensions; context.BulkMerge(customers, options => { // ON UPDATE: only update if "Name" or "Email" is different between source and destination options.MergeMatchedAndOneNotConditionExpression = x => new { x.Name, x.Email }; });
Typical scenarios include:
- Audit fields (
ModifiedDate
,ModifiedBy
) β Only update when at least one of these changes, skipping updates that donβt touch metadata. - Contact info (
Phone
,Email
) β Ensure updates are made only when at least one contact detail is different, ignoring other non-critical fields. - Product pricing (
Price
,Discount
) β Update records only if the pricing-related values have changed, skipping cosmetic changes likeDescription
. - User profile (
Name
,Email
,Role
) β Prevent unnecessary updates unless a meaningful field like name, email, or role has been modified.
This option applies to the following methods in Entity Framework Extensions:
π‘ Example Effect
ID | Destination.Name | Source.Name | Destination.Email | Source.Email | Without Condition | With Condition |
---|---|---|---|---|---|---|
1 | John | John | john@a.com | john@a.com | updated | skipped |
2 | John | Johnny | john@a.com | john@a.com | updated | updated |
3 | John | John | john@a.com | johnny@a.com | updated | updated |
π οΈ 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 MatchedAndOneNotCondition
option from Entity Framework Extensions when:
- You want to update only if at least one key field has changed
- You need to ignore changes in unimportant fields
- You want to optimize synchronization by skipping updates when nothing meaningful has changed
β Why Itβs Useful
Without MatchedAndOneNotCondition
, using in EF Core a bulk operations like BulkUpdate, BulkMerge, or BulkSynchronize from Entity Framework Extensions could update rows unnecessarily, even when the data hasnβt really changed.
Using this option, you can:
- Prevent redundant updates
- Focus updates only on meaningful differences
- Simplify logic and avoid writing custom SQL
π’ Scenario
A company imports customers using BulkMerge method from Entity Framework Extensions in EF Core.
- The
Note
column often changes, but itβs not important enough to trigger an update. - The system should only update a row when critical fields like
Name
orEmail
are different.
In summary:
- If
Name
orEmail
differs β update the customer - If both are equal β skip the update, even if other fields (like
Note
) changed
ποΈ Solution
The MatchedAndOneNotCondition
option offers four ways to configure behavior:
- [Action]MatchedAndOneNotConditionExpression β include properties via a lambda expression
- [Action]MatchedAndOneNotConditionNames β include properties via a list of property names
- IgnoreOn[Action]MatchedAndOneNotConditionExpression β exclude properties via a lambda expression, all others properties are included.
- IgnoreOn[Action]MatchedAndOneNotConditionNames β exclude properties via a list of property names, all others properties are included.
π·οΈ [Action]MatchedAndOneNotConditionExpression
Use this option to specify β with a lambda expression β which property values should be compared. If at least one property value is different, the update is executed.
context.BulkMerge(customers, options => { // ON UPDATE: perform update only when "Name" or "Email" differ between source and destination options.MergeMatchedAndOneNotConditionExpression = x => new { x.Name, x.Email }; });
Method | Option Name | Try it |
---|---|---|
BulkMerge | MergeMatchedAndOneNotConditionExpression | Online Example |
BulkUpdate | UpdateMatchedAndOneNotConditionExpression | Online Example |
BulkSynchronize | SynchronizeMatchedAndOneNotConditionExpression | Online Example |
π·οΈ [Action]MatchedAndOneNotConditionNames
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 update is executed.
context.BulkMerge(customers, options => { options.MergeMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.Name), nameof(Customer.Email) }; });
Method | Option Name | Try it |
---|---|---|
BulkMerge | MergeMatchedAndOneNotConditionNames | Online Example |
BulkUpdate | UpdateMatchedAndOneNotConditionNames | Online Example |
BulkSynchronize | SynchronizeMatchedAndOneNotConditionNames | Online Example |
π·οΈ IgnoreOn[Action]MatchedAndOneNotConditionExpression
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 update is executed.
context.BulkMerge(customers, options => { // Exclude Note β it wonβt trigger updates options.IgnoreOnMergeMatchedAndOneNotConditionExpression = x => new { x.Note }; });
Method | Option Name | Try it |
---|---|---|
BulkMerge | IgnoreOnMergeMatchedAndOneNotConditionExpression | Online Example |
BulkUpdate | IgnoreOnUpdateMatchedAndOneNotConditionExpression | Online Example |
BulkSynchronize | IgnoreOnSynchronizeMatchedAndOneNotConditionExpression | Online Example |
π·οΈ IgnoreOn[Action]MatchedAndOneNotConditionNames
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 update is executed.
context.BulkMerge(customers, options => { options.IgnoreOnMergeMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.Note) }; });
Method | Option Name | Try it |
---|---|---|
BulkMerge | IgnoreOnMergeMatchedAndOneNotConditionNames | Online Example |
BulkUpdate | IgnoreOnUpdateMatchedAndOneNotConditionNames | Online Example |
BulkSynchronize | IgnoreOnSynchronizeMatchedAndOneNotConditionNames | Online Example |
π Conclusion
Using MatchedAndOneNotCondition
option in Entity Framework Extensions with EF Core ensures that updates only happen when at least one important value is different between source and destination.
You can choose:
- Expression-based include β
[Action]MatchedAndOneNotConditionExpression
- Name-based include β
[Action]MatchedAndOneNotConditionNames
- Expression-based exclude β
IgnoreOn[Action]MatchedAndOneNotConditionExpression
- Name-based exclude β
IgnoreOn[Action]MatchedAndOneNotConditionNames
By using MatchedAndOneNotCondition
in your bulk operations:
- Skip redundant updates when values are identical
- Focus updates only on fields that matter
- Keep imports and synchronizations faster and more predictable
π Related Articles
Column Options
Coalesce Options
Matched Options
Delete Matched Options
ZZZ Projects