facebook

Entity Framework Extensions Matched and one not Condition

Description

The MatchedAndOneNotCondition option lets you perform or skip the update action, depending on if at least one value from the source is different than the destination for properties specified.

Example

context.BulkMerge(customers, options => 
{
	// USE the code as the key expression
	options.ColumnPrimaryKeyExpression = x => x.Code;
	
	// ON UPDATE, modify customers only if a values such for the Code or Name has been modified
	options.MergeMatchedAndOneNotConditionExpression = x => new { x.Code, x.Name };
});

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 Note column is not really important and should not trigger an update action if this is the only value that has been modified.

The update action should only be performed if another values such as the Name or Email has been modified.

In summary:

  • If only the Note value is different, the customer should not be updated
  • If another value is different, the customer can be updated

Solution

TheMatchedAndOneNotCondition option have 4 solutions to this problem:

[Action]MatchedAndOneNotConditionExpression

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

context.BulkMerge(customers, options => 
{
	// USE the code as the key expression
	options.ColumnPrimaryKeyExpression = x => x.Code;
	
	// ON UPDATE, modify customers only if a values such for the Code or Name has been modified
	options.MergeMatchedAndOneNotConditionExpression = x => new { x.Code, x.Name };
});
Method Name Try it
BulkMerge MergeMatchedAndOneNotConditionExpression Fiddle
BulkUpdate UpdateMatchedAndOneNotConditionExpression Fiddle
BulkSynchronize SynchronizeMatchedAndOneNotConditionExpression Fiddle

[Action]MatchedAndOneNotConditionNames

Use this option if you prefer to specify a list of properties names you want to include. The value must correspond to the property name or the navigation name.

context.BulkMerge(customers, options => 
{
	// USE the code as the key expression
	options.ColumnPrimaryKeyExpression = x => x.Code;
	
	// ON UPDATE, modify customers only if a values such for the Code or Name has been modified
	options.MergeMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.Code), nameof(Customer.Name) };
});
Method Name Try it
BulkMerge MergeMatchedAndOneNotConditionNames Fiddle
BulkUpdate UpdateMatchedAndOneNotConditionNames Fiddle
BulkSynchronize SynchronizeMatchedAndOneNotConditionNames Fiddle

IgnoreOn[Action]MatchedAndOneNotConditionExpression

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 => 
{
	// USE the code as the key expression
	options.ColumnPrimaryKeyExpression = x => x.Code;
	
	// ON UPDATE, modify customers only if a values such for the Code or Name has been modified by excluding all other properties
	options.IgnoreOnMergeMatchedAndOneNotConditionExpression = x => new { x.Note };
});
Method Name Try it
BulkMerge IgnoreOnMergeMatchedAndOneNotConditionExpression Fiddle
BulkUpdate IgnoreOnUpdateMatchedAndOneNotConditionExpression Fiddle
BulkSynchronize IgnoreOnSynchronizeMatchedAndOneNotConditionExpression Fiddle

IgnoreOn[Action]MatchedAndOneNotConditionNames

Use this option if you prefer to specify a list of properties 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 => 
{
	// USE the code as the key expression
	options.ColumnPrimaryKeyExpression = x => x.Code;
	
	// ON UPDATE, modify customers only if a values such for the Code or Name has been modified by excluding all other properties
	options.IgnoreOnMergeMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.Note) };
});
Method Name Try it
BulkMerge IgnoreOnMergeMatchedAndOneNotConditionNames Fiddle
BulkUpdate IgnoreOnUpdateMatchedAndOneNotConditionNames Fiddle
BulkSynchronize IgnoreOnSynchronizeMatchedAndOneNotConditionNames Fiddle