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 => { // ON UPDATE, modify the customer if a values for "Name" or "Email" is different options.MergeMatchedAndOneNotConditionExpression = x => new { x.Name, x.Email }; });
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 the
Name
orEmail
value is different to the database, the customer can be updated - If the
Name
orEmail
value is equal to the database, the customer cannot be updated
Solution
TheMatchedAndOneNotCondition
option have 4 solutions to this problem:
- [Action]MatchedAndOneNotConditionExpression
- [Action]MatchedAndOneNotConditionNames
- IgnoreOn[Action]MatchedAndOneNotConditionExpression
- IgnoreOn[Action]MatchedAndOneNotConditionNames
[Action]MatchedAndOneNotConditionExpression
Use this option if you prefer to specify with an expression which properties you want to include.
context.BulkMerge(customers, options => { // ON UPDATE, modify the customer if a values for "Name" or "Email" is different options.MergeMatchedAndOneNotConditionExpression = x => new { x.Name, x.Email }; });
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 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 the customer if a values for "Name" or "Email" is different options.MergeMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.Name), nameof(Customer.Email) }; });
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 => { // ON UPDATE, modify the customer if a values for "Name" or "Email" is different (by excluding 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 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 the customer if a values for "Name" or "Email" is different (by excluding other properties) options.IgnoreOnMergeMatchedAndOneNotConditionNames = new List<string>() { nameof(Customer.Note) }; });
Method | Name | Try it |
---|---|---|
BulkMerge | IgnoreOnMergeMatchedAndOneNotConditionNames | Fiddle |
BulkUpdate | IgnoreOnUpdateMatchedAndOneNotConditionNames | Fiddle |
BulkSynchronize | IgnoreOnSynchronizeMatchedAndOneNotConditionNames | Fiddle |
Related Solutions
ZZZ Projects