Thinktecture.EntityFrameworkCore icon indicating copy to clipboard operation
Thinktecture.EntityFrameworkCore copied to clipboard

Thinktecture.EntityFrameworkCore.BulkOperations throws exception when excluding properties

Open herascu opened this issue 1 year ago • 1 comments
trafficstars

When trying to set the argument to exclude properties on BulkInsert, this exception gets thrown: Complex projections are not supported. Current expression: value(Program+<>c__DisplayClass0_0).propertiesToExclude at Thinktecture.BulkOperationsExpressionExtensions.ExtractMember(ParameterExpression paramExpression, MemberExpression memberAccess) at Thinktecture.BulkOperationsExpressionExtensions.ExtractMembers(List1 members, ParameterExpression paramExpression, Expression body) at Thinktecture.BulkOperationsExpressionExtensions.ExtractMembers[T](Expression1 projection) at Thinktecture.EntityFrameworkCore.BulkOperations.IEntityPropertiesProvider.Exclude[T](Expression`1 projection)

Sample code to replicate - simple C# console app with latest EF Core 8 and SQL Server 2019: var entities = new List<DataTable>(); Expression<Func<DataTable, object>> propertiesToExclude = p => p.RowDateTime; var propertiesToInsertValue = IEntityPropertiesProvider.Exclude<DataTable>(_ => propertiesToExclude); var options = new SqlServerBulkInsertOptions { PropertiesToInsert =propertiesToInsertValue, EnableStreaming = true, BulkCopyTimeout = TimeSpan.FromMinutes(5) }; await DbContext.BulkInsertAsync(entities, options);

public class DataTable { public int Id { get; set; } public string Name { get; set; } public DateTime RowDateTime { get; set; } }

herascu avatar Apr 25 '24 20:04 herascu

In your code example you provide a delegate which returns a delegate:

Expression<Func<DataTable, object>> propertiesToExclude = p => p.RowDateTime;
var propertiesToInsertValue = IEntityPropertiesProvider.Exclude<DataTable>(_ => propertiesToExclude);

// which is something like this:
var propertiesToInsertValue = IEntityPropertiesProvider.Exclude<DataTable>(_ => (p => p.RowDateTime));

The method exclude expects a property or an (anon) object:

var propertiesToInsertValue = IEntityPropertiesProvider.Exclude<DataTable>(p => p.RowDateTime);

// or
var propertiesToInsertValue = IEntityPropertiesProvider.Exclude<DataTable>(p => new { p.RowDateTime });

PawelGerr avatar Apr 26 '24 05:04 PawelGerr