Skip to content

BulkInsertOptimizedAsync does not recognize EF Core shadow properties - Missing Column error #637

@Valem

Description

@Valem

Description

We are implementing a delta tracking feature for master data replication where we want to store a hash of each entity's properties for change detection. We use a shadow property called _Hash (varchar(16)) which is configured on all entity types in our model.

The shadow property is configured via ModelBuilder and works correctly with standard EF Core operations. However, when using BulkInsertOptimizedAsync, we receive the following exception:

System.Exception: An error occured while resolving mapping by name. See the inner exception for details
 ---> System.Exception: Missing Column : _Hash
On entity : Vendor
On Table : [mdb].[Vendor]
   at Z.BulkOperations.BulkOperation...

The column _Hash does exist in the database table (confirmed via migration). The issue is that BulkInsertOptimizedAsync appears to use CLR reflection to identify columns rather than EF Core's metadata/ChangeTracker, so it doesn't see the shadow property.

Our Setup

Shadow property configuration:

public static ModelBuilder ConfigureDeltaTracking(this ModelBuilder modelBuilder)
{
    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    {
        if (entityType.ClrType != null && !entityType.IsOwned())
        {
            var entityBuilder = modelBuilder.Entity(entityType.ClrType);

            entityBuilder
                .Property<string>("_Hash")
                .HasColumnType("varchar(16)")
                .IsRequired(false);

            entityBuilder
                .HasIndex("_Hash")
                .IncludeProperties("Id");
        }
    }

    return modelBuilder;
}

Setting the shadow property value before bulk insert:

foreach (var entity in batch)
{
    var hash = _hashGenerator.ComputeHash(entity);
    var entry = localContext.Entry(entity);
    entry.Property("_Hash").CurrentValue = hash;
}

await localContext.BulkInsertOptimizedAsync(batch, cancellationToken);

Expected Behavior

BulkInsertOptimizedAsync should recognize shadow properties configured in EF Core's model and include them in the bulk insert operation, similar to how the regular BulkInsert method handles them (we assume).

Actual Behavior

The operation fails with "Missing Column: _Hash" because the library uses CLR type reflection instead of EF Core's model metadata to discover columns.

Question

Is there a way to make BulkInsertOptimizedAsync recognize and include shadow properties?

Workaround Attempted

We are currently working around this by converting the shadow property to a regular CLR property on an interface (IHashTrackableEntity), but this pollutes our domain model with infrastructure concerns. We would prefer to keep using shadow properties if possible.

Further technical details

  • EF Core version: 8.1.0
  • EF Extensions version: 8.105.0
  • Database Provider: SQL Server (Azure SQL Database)
  • Method used: BulkInsertOptimizedAsync

Thank you for your assistance!

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions