Entity Framework Extensions Matched and Condition

Description

The MatchedAndCondition option lets you perform or skip the update action, depending on if all values from the source and destination are equals for properties specified.

Example

context.BulkMerge(customers, options => 
{
	// ON UPDATE, modify customers that has the same `IsLocked` value (always "false" on the source)
	options.MergeMatchedAndConditionExpression = x => new { x.IsLocked };
});

Scenario

A company uses Entity Framework and needs to import customers with the BulkMerge method to insert new customers and update existing customers.

However, there is a particularity. The customer has a column IsLocked in the database and any customer locked should not be updated.

  • When IsLocked = 0, the customer can be updated
  • When IsLocked = 1, the customer cannot be updated

All customers to import have the value IsLocked = true; // 0, so the update action should only be performed when both IsLocked values (source and destination) are equals.

Note: We cannot use the PrimaryKey option in this scenario. Otherwise, when performing a BulkMerge, it will consider the locked customer as a new customer instead of an existing one and insert it.

Solution

TheMatchedAndCondition option has 4 solutions to this problem:

[Action]MatchedAndConditionExpression

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

context.BulkMerge(customers, options => 
{
	// ON UPDATE, modify customers that has the same `IsLocked` value (always "false" on the source)
	options.MergeMatchedAndConditionExpression = x => new { x.IsLocked };
});
Method Name Try it
BulkMerge MergeMatchedAndConditionExpression Fiddle
BulkUpdate UpdateMatchedAndConditionExpression Fiddle
BulkSynchronize SynchronizeMatchedAndConditionExpression Fiddle

[Action]MatchedAndConditionNames

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.

context.BulkMerge(customers, options => 
{
	// ON UPDATE, modify customers that has the same `IsLocked` value (always "false" on the source)
	options.MergeMatchedAndConditionNames = new List<string>() { nameof(Customer.IsLocked) };
});
Method Name Try it
BulkMerge MergeMatchedAndConditionNames Fiddle
BulkUpdate UpdateMatchedAndConditionNames Fiddle
BulkSynchronize SynchronizeMatchedAndConditionNames Fiddle

IgnoreOn[Action]MatchedAndConditionExpression

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.

context.BulkMerge(customers, options => 
{
	// ON UPDATE, modify customers that has the same `IsLocked` value (always "false" on the source) (by excluding other properties)
	options.IgnoreOnMergeMatchedAndConditionExpression = x => new { x.CustomerID, x.Name, x.Email, x.Note };
});
Method Name Try it
BulkMerge IgnoreOnMergeMatchedAndConditionExpression Fiddle
BulkUpdate IgnoreOnUpdateMatchedAndConditionExpression Fiddle
BulkSynchronize IgnoreOnSynchronizeMatchedAndConditionExpression Fiddle

IgnoreOn[Action]MatchedAndConditionNames

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.

context.BulkMerge(customers, options => 
{
	// ON UPDATE, modify customers that has the same `IsLocked` value (always "false" on the source) (by excluding other properties)
	options.IgnoreOnMergeMatchedAndConditionNames = new List<string>() { nameof(Customer.CustomerID), nameof(Customer.Name), nameof(Customer.Email), nameof(Customer.Note) };
});
Method Name Try it
BulkMerge IgnoreOnMergeMatchedAndConditionNames Fiddle
BulkUpdate IgnoreOnUpdateMatchedAndConditionNames Fiddle
BulkSynchronize IgnoreOnSynchronizeMatchedAndConditionNames Fiddle