Entity Framework Extensions Use Custom Key


You want to perform a Bulk Operation (BulkUpdate, BulkDelete, or BulkMerge) but using a different key than the one specified.

For example, you want to perform a BulkUpdate on a customer list but using the customer "Code" instead of the "CustomerID" for the key.


You can specify the column to use for the key with the ColumnPrimaryKeyExpression options.


// Single Key
ctx.BulkUpdate(customers, operation => operation.ColumnPrimaryKeyExpression =
    customer => customer.Code);

// Surrogate Key (with anonymous type)
ctx.BulkUpdate(customers, operation => operation.ColumnPrimaryKeyExpression =
    customer => new { customer.Code1, customer.Code2, customer.Code3 });


Duplicate Key

You receive the following error:

An error occurred because the primary key specified is not unique, you can set the property 'AllowDuplicateKeys' to true to allow duplicate keys.

This error happens because the key is used in more than one row in the source


Code Name
001 Jon
002 Henri
001 Jonathan

The code "001" is here more than once with a different name.

You can allow duplicate key with the AllowDuplicateKeys options.

ctx.BulkUpdate(customers, operation =>
    operation.AllowDuplicateKeys = true;
    operation.ColumnPrimaryKeyExpression = customer => customer.Code;

Last updated: 2023-02-25