Search code examples
c#entity-frameworkentity-framework-coreentity-framework-extensions

BulkSynchronize a subset of data


I'm using EF extensions and want to synchronise a subset of data.

For example my table:

|Type|Value|
------------
|A   |1    |
|B   |2    |
|B   |3    |
|A   |4    |
|A   |5    |

New data comes

|Type|Value|
------------
|B   |6    |
|B   |7    |
|B   |8    |

And I want to replace all Bs without touching As to get

|Type|Value|
------------
|A   |1    |
|B   |6    |
|B   |7    |
|B   |8    |
|A   |4    |
|A   |5    |

Is there any way to achieve that using bulk operations?


Solution

  • Disclaimer: I'm the owner of the project Entity Framework Extensions

    You are looking for the ColumnSynchronizeDeleteKeySubsetExpression options.

    For example, only type that will be equal to a type from your new data (so only B type) will be deleted:

    ctx.BulkSynchronize(list, options => options.ColumnSynchronizeDeleteKeySubsetExpression = c  => c.Type);
    

    Let me know if you need some help to implement it.