πŸ”’β“ Matched and Condition Option in Entity Framework ExtensionsUpdate only when source and destination values match

The MatchedAndCondition option in Entity Framework Extensions lets you perform the UPDATE action only when certain values in the source and destination are equal.

If the selected values don’t match, the update is skipped. This is useful whenever you need more control over which rows get updated β€” for example to avoid touching restricted records, skip already-approved entries, or enforce simple concurrency checks.

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

context.BulkMerge(customers, options => 
{
    // ON UPDATE: only update customers if "IsLocked" has the same value in both source and destination
    options.MergeMatchedAndConditionExpression = x => new { x.IsLocked };
});

Typical scenarios include:

  • Locked records (IsLocked) β†’ Don’t update customers that are marked as locked in the database.
  • Status field (Status) β†’ Only update if both records are still Active, skipping updates if the destination has been moved to Archived or Closed.
  • Approval date (ApprovalDate) β†’ Prevent updates once a row has been approved (only update when both are null).
  • Row version (Version) β†’ Use as a lightweight concurrency check β€” update only if both source and destination have the same version value.

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


πŸ’‘ Example Effect

ID Destination.IsLocked Source.IsLocked Without Condition With Condition
1 0 (unlocked) 0 (unlocked) updated updated
2 1 (locked) 0 (unlocked) updated skipped
3 0 (unlocked) 1 (locked) updated 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 MatchedAndCondition option from Entity Framework Extensions when:

  • You want to skip updates for certain records based on business rules
  • Updates should happen only when both source and destination values match
  • Primary key filtering alone is not enough, and you need finer control

⭐ Why It’s Useful

Without MatchedAndCondition, using in EF Core a bulk operations like BulkUpdate, BulkMerge, or BulkSynchronize from Entity Framework Extensions could accidentally update rows that should not change.

Using this option, you can:

  • Protect sensitive or restricted data
  • Avoid overwriting approved or finalized rows
  • Simplify your logic without writing custom SQL conditions

🏒 Scenario

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

In the example, customers have an IsLocked column:

  • IsLocked = 0 β†’ the customer can be updated
  • IsLocked = 1 β†’ the customer must not be updated

All source customers have IsLocked = 0. Without precautions, locked customers could still be updated.

With the MatchedAndCondition option, updates only occur when both values match, ensuring locked customers stay unchanged.

Other common cases include:

  • Updating orders only if the Status values are the same in both systems
  • Updating records only if the ApprovalDate values are both null
  • Updating entities only when the Version values match, to prevent overwriting concurrent changes

πŸ—οΈ Solution

The MatchedAndCondition option offers four ways to configure behavior:


🏷️ [Action]MatchedAndConditionExpression

Use this option to specify β€” with a lambda expression β€” which property values must be equal in both source and destination for the UPDATE to occur.

If all selected property values match, the update is executed. Otherwise, it is skipped.

context.BulkMerge(customers, options => 
{
    // ON UPDATE: perform update only when "IsLocked" values are equal in both source and destination
    options.MergeMatchedAndConditionExpression = x => new { x.IsLocked };
});
Method Option Name Try it
BulkMerge MergeMatchedAndConditionExpression Online Example
BulkUpdate UpdateMatchedAndConditionExpression Online Example
BulkSynchronize SynchronizeMatchedAndConditionExpression Online Example

🏷️ [Action]MatchedAndConditionNames

Use this option to specify β€” with a list of property names β€” which property values must be equal in both source and destination for the UPDATE to occur.

If all selected property values match, the update is executed. Otherwise, it is skipped.

context.BulkMerge(customers, options => 
{
    // ON UPDATE: perform update only when "IsLocked" values are equal in both source and destination
    options.MergeMatchedAndConditionNames = new List<string>() { nameof(Customer.IsLocked) };
});
Method Option Name Try it
BulkMerge MergeMatchedAndConditionNames Online Example
BulkUpdate UpdateMatchedAndConditionNames Online Example
BulkSynchronize SynchronizeMatchedAndConditionNames Online Example

🏷️ IgnoreOn[Action]MatchedAndConditionExpression

Use this option to specify β€” with a lambda expression β€” which property values should be excluded from the equality check.

All other property values (not listed) will be compared between source and destination. The UPDATE will only happen if those remaining values are equal.

context.BulkMerge(customers, options => 
{
    // ON UPDATE: perform update only when all values except these are equal
    options.IgnoreOnMergeMatchedAndConditionExpression = x => new { x.CustomerID, x.Name, x.Email };
});
Method Option Name Try it
BulkMerge IgnoreOnMergeMatchedAndConditionExpression Online Example
BulkUpdate IgnoreOnUpdateMatchedAndConditionExpression Online Example
BulkSynchronize IgnoreOnSynchronizeMatchedAndConditionExpression Online Example

🏷️ IgnoreOn[Action]MatchedAndConditionNames

Use this option to specify β€” with a list of property names β€” which property values should be excluded from the equality check.

All other property values (not listed) will be compared between source and destination. The UPDATE will only happen if those remaining values are equal.

context.BulkMerge(customers, options => 
{
    // ON UPDATE: perform update only when all values except these are equal
    options.IgnoreOnMergeMatchedAndConditionNames = new List<string>() 
    { 
        nameof(Customer.CustomerID), nameof(Customer.Name), nameof(Customer.Email) 
    };
});
Method Option Name Try it
BulkMerge IgnoreOnMergeMatchedAndConditionNames Online Example
BulkUpdate IgnoreOnUpdateMatchedAndConditionNames Online Example
BulkSynchronize IgnoreOnSynchronizeMatchedAndConditionNames Online Example

🏁 Conclusion

Using MatchedAndCondition option in Entity Framework Extensions with EF Core ensures that updates only happen when specific source and destination values match.

You can choose:

  • Expression-based include β†’ [Action]MatchedAndConditionExpression
  • Name-based include β†’ [Action]MatchedAndConditionNames
  • Expression-based exclude β†’ IgnoreOn[Action]MatchedAndConditionExpression
  • Name-based exclude β†’ IgnoreOn[Action]MatchedAndConditionNames

By using MatchedAndCondition in your bulk operations:

  • Prevent unwanted updates when values don’t meet your business rules
  • Fine-tune which records should or should not be updated
  • Simplify synchronization logic and keep your database consistent

Column Options

Coalesce Options

Matched Options

Delete Matched Options


Last updated: 2025-08-17
Author: