How to Configure Column Options in Entity Framework ExtensionsA Beginner’s Guide
In the previous article, we looked at how to configure options in Entity Framework Extensions.
In this article, we’ll go deeper and explain the basics of Column Options, which let you configure things like:
- Which primary key to use
- Which properties to include
- Which properties to exclude
- Whether to use hardcoded SQL
For many options, we offer three different suffixes:
- Expression — Define the mapping using either a simple lambda expression or a lambda with a body
- Names — Pass a
List<string>containing property names as strings - Formula — Use hardcoded SQL to take full control over that part of the query
Options with the same name but different suffixes are usually mutually exclusive.
For example, you can use either ColumnPrimaryKeyExpression or ColumnPrimaryKeyNames, but not both.
📌 Quick Reference — When to Use Each Suffix
| Suffix | What It Uses | Best For | Example |
|---|---|---|---|
| Expression | Lambda expressions referencing entity properties | Strongly-typed mappings in code | x => x.ID or x => new { x.ID, x.ID2 } |
| Names | List of property names as strings | Dynamic or external property lists | new List<string> { nameof(MyEntity.ID) } |
| Formula | Hardcoded SQL fragments | Advanced SQL control (use carefully) | "NOT DestinationTable.IsLocked" |
Options with the "Expression" Suffix
Options with the "Expression" suffix, like ColumnPrimaryKeyExpression, let you define the mapping using either:
- A simple lambda expression — can only be used when mapping one property
- A lambda with a body — can be used to map one or more properties
// @nuget: Z.EntityFramework.Extensions.EFCore using Z.EntityFramework.Extensions; // Single property (simple lambda) context.BulkUpdate(list, options => { options.ColumnPrimaryKeyExpression = x => x.ID; }); // Multiple properties (lambda with a body) context.BulkUpdate(list, options => { options.ColumnPrimaryKeyExpression = x => new { x.ID, x.ID2 }; });
Common Options with the "Expression" Suffix
Below is the complete list of options in Entity Framework Extensions that use the "Expression" suffix:
-
ColumnPrimaryKeyExpression
-
ColumnInputExpressionColumnInputOutputExpressionColumnOutputExpressionOnMergeInsertInputExpressionOnMergeUpdateInputExpressionOnSynchronizeInsertInputExpressionOnSynchronizeUpdateInputExpressionIgnoreColumnOutputExpressionIgnoreOnInsertExpressionIgnoreOnUpdateExpressionIgnoreOnMergeInsertExpressionIgnoreOnMergeUpdateExpressionIgnoreOnSynchronizeInsertExpressionIgnoreOnSynchronizeUpdateExpression
Auto Mapping
AutoMapIdentityExpressionAutoMapKeyExpression
-
CoalesceOnUpdateExpressionCoalesceOnMergeUpdateExpressionCoalesceOnSynchronizeUpdateExpression
-
CoalesceDestinationOnUpdateExpressionCoalesceDestinationOnMergeUpdateExpressionCoalesceDestinationOnSynchronizeUpdateExpression
-
UpdateMatchedAndConditionExpressionDeleteMatchedAndConditionExpressionMergeMatchedAndConditionExpressionSynchronizeMatchedAndConditionExpressionIgnoreOnUpdateMatchedAndConditionExpressionIgnoreOnDeleteMatchedAndConditionExpressionIgnoreOnMergeMatchedAndConditionExpressionIgnoreOnSynchronizeMatchedAndConditionExpression
-
UpdateMatchedAndOneNotConditionExpressionDeleteMatchedAndOneNotConditionExpressionMergeMatchedAndOneNotConditionExpressionSynchronizeMatchedAndOneNotConditionExpressionIgnoreOnUpdateMatchedAndOneNotConditionExpressionIgnoreOnDeleteMatchedAndOneNotConditionExpressionIgnoreOnMergeMatchedAndOneNotConditionExpressionIgnoreOnSynchronizeMatchedAndOneNotConditionExpression
Misc
ColumnAddOrUpdateInputExpressionColumnAddOrUpdateInputOutputExpressionColumnAddOrUpdateOutputExpressionColumnStagingTableFormulaExpressionColumnSynchronizeDeleteKeySubsetExpressionQueryFilterPrimaryKeyExpression
Options with the "Names" Suffix
Options with the "Names" suffix, like ColumnPrimaryKeyNames, let you create a List<string> and pass property names as strings.
We always recommend using the nameof operator whenever possible — this helps prevent typos and avoids breaking your code during refactoring.
// @nuget: Z.EntityFramework.Extensions.EFCore using Z.EntityFramework.Extensions; // Single property from an external list (dynamic source) var externalKeyList = new List<string> { "ID" }; context.BulkUpdate(list, options => { options.ColumnPrimaryKeyNames = externalKeyList; }); // Multiple properties using nameof() context.BulkUpdate(list, options => { options.ColumnPrimaryKeyNames = new List<string> { nameof(MyEntity.ID), nameof(MyEntity.ID2) }; });
Common Options with the "Names" Suffix
Below is the complete list of options in Entity Framework Extensions that use the "Names" suffix:
-
ColumnPrimaryKeyNames
-
ColumnInputNamesColumnInputOutputNamesColumnOutputNamesOnMergeInsertInputNamesOnMergeUpdateInputNamesOnSynchronizeInsertInputNamesOnSynchronizeUpdateInputNamesIgnoreOnInsertNamesIgnoreColumnOutputNamesIgnoreOnUpdateNamesIgnoreOnMergeInsertNamesIgnoreOnMergeUpdateNamesIgnoreOnSynchronizeInsertNamesIgnoreOnSynchronizeUpdateNames
Auto Mapping
AutoMapIdentityName(Note: this property is astring)AutoMapKeyName(Note: this property is astring)
-
CoalesceOnUpdateNamesCoalesceOnMergeUpdateNamesCoalesceOnSynchronizeUpdateNames
-
CoalesceDestinationOnUpdateNamesCoalesceDestinationOnMergeUpdateNamesCoalesceDestinationOnSynchronizeUpdateNames
-
UpdateMatchedAndConditionNamesDeleteMatchedAndConditionNamesMergeMatchedAndConditionNamesSynchronizeMatchedAndConditionNamesIgnoreOnUpdateMatchedAndConditionNamesIgnoreOnDeleteMatchedAndConditionNamesIgnoreOnMergeMatchedAndConditionNamesIgnoreOnSynchronizeMatchedAndConditionNames
-
UpdateMatchedAndOneNotConditionNamesDeleteMatchedAndOneNotConditionNamesMergeMatchedAndOneNotConditionNamesSynchronizeMatchedAndOneNotConditionNamesIgnoreOnUpdateMatchedAndOneNotConditionNamesIgnoreOnDeleteMatchedAndOneNotConditionNamesIgnoreOnMergeMatchedAndOneNotConditionNamesIgnoreOnSynchronizeMatchedAndOneNotConditionNames
Misc
ColumnAddOrUpdateInputNamesColumnAddOrUpdateInputOutputNamesColumnAddOrUpdateOutputNamesColumnStagingTableFormulaNamesColumnSynchronizeDeleteKeySubsetNamesQueryFilterPrimaryKeyNames
Options with the "Formula" Suffix
Options with the "Formula" suffix, like MergeMatchedAndFormula, let you hardcode a SQL fragment that will be inserted directly into the generated SQL statement.
// @nuget: Z.EntityFramework.Extensions.EFCore using Z.EntityFramework.Extensions; // Use a formula to avoid updating "locked" rows context.BulkMerge(list, options => { options.MergeMatchedAndFormula = "NOT DestinationTable.IsLocked"; });
⚠️ Security Warning: Never use hardcoded SQL that comes from user input. You should always have full control over any SQL written in a formula to prevent SQL injection.
Common Options with the "Formula" Suffix
Below is the complete list of options in Entity Framework Extensions that use the "Formula" suffix:
-
InsertPrimaryKeyAndFormulaUpdatePrimaryKeyAndFormulaDeletePrimaryKeyAndFormulaMergePrimaryKeyAndFormula
Staging Table Filter
InsertStagingTableFilterFormulaUpdateStagingTableFilterFormulaDeleteStagingTableFilterFormulaMergeStagingTableFilterFormula
-
UpdateMatchedAndFormulaDeleteMatchedAndFormulaMergeMatchedAndFormulaSynchronizeMatchedAndFormula
Not Matched And Formula
InsertNotMatchedAndFormulaMergeNotMatchedAndFormulaSynchronizeNotMatchedAndFormula
Misc
ColumnSynchronizeDeleteKeySubsetFormulaSynchronizeDeleteDestinationTableFilterFormulaSynchronizeSoftDeleteFormulaQueryFilterPrimaryKeyAndFormula
Conclusion
Column Options give you full control over how your data is saved with Entity Framework Extensions. By choosing between Expression, Names, and Formula suffixes, you can decide exactly which columns to use, how to map them, and even inject your own SQL logic when needed.
- Expression is best when working directly with entity properties in code.
- Names is useful when you need to pass property names dynamically.
- Formula is the most powerful, letting you control the generated SQL, but should be used carefully.
With these options, you can go beyond the default SaveChanges behavior and adapt your insert, update, or merge operations to match your exact business rules.
Author: ZZZ Projects