EntityFramework-Extensions icon indicating copy to clipboard operation
EntityFramework-Extensions copied to clipboard

Error on BulkInsert with EF6.4 - Wrong type information is used for converting related (FK) entities identifiers

Open mmiller678 opened this issue 5 years ago • 2 comments

Description

When using EF6 and BulkInsert entities with mixed identifier types seems to cause issues.

var payment = new Payment() //Payment.Id is of type long, database identity
{
  CreditCard = new CreditCard { Id = 45 }, //CreditCard.Id is of type long
  Currency = new Currency { Id = "USD" }, //Currency.Id is of type string
  Amount = 109.98
};
context.BulkInsert(new[]{ payment });

The BulkInsert code is erroring as its trying to convert "USD" to a long (see exception below). A few thing to note:

  1. All entities derive from the same generic base class Entity<T> where T is the type of the Id field. My guess is this is what is causing the problem and the bulk insert code is examining type information assumes its know the type of Entity.Id but it not taking into account that property type for ID is driven by a generic and therefore will change based on the concrete type.
  2. EF mapping configurations are explicitly specified for each type
  3. All other EF and EFE batch functions are working fine for the entire domain model (including a slew of integration tests)

Exception

Stack trace: System.Exception USD is not a valid value for Int64. at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at .(Object this, Type ) at .[](DbContext this, List1 ,  , SchemaEntityType , Int32 , List1 , Boolean , List1 ) at DbContextExtensions.1.( ) at .Read() at .() at .( , DbCommand ) at .( , DbCommand ) at .Execute(List1 actions) at Z.BulkOperations.BulkOperation.Execute() at Z.EntityFramework.Extensions.EntityBulkOperation1.BulkInsert() at .BulkInsert[T](BulkOperation1 this, DbContext context, List1 list, Boolean isManager, List1 entitiesToUpdate, Type type, String typeName) at .1.(SchemaEntityType ) at System.Collections.Generic.List1.ForEach(Action1 action) at .BulkInsert[T](DbContext this, BulkOperation1 bulkOperation, IEnumerable1 entities2, List1 entitiesToUpdate) at DbContextExtensions.BulkInsert[T](DbContext this, IEnumerable1 entities, Action`1 bulkOperationFactory)

Further technical details

  • EF version: 6.4.4
  • EF Extensions version: 1.12.23
  • Database Provider: MS SQL Server

mmiller678 avatar Jul 10 '20 19:07 mmiller678

Hello @mmiller678 ,

Thank you for reporting, we will try to reproduce it.

Best Regards,

Jon

JonathanMagnan avatar Jul 11 '20 22:07 JonathanMagnan

Hello @mmiller678 ,

The v4.0.82 has been released.

Could you try it and let us know if everything is now working as expected.

Best Regards,

Jon

JonathanMagnan avatar Jul 14 '20 23:07 JonathanMagnan