zzzprojects / EntityFramework-Effort

Entity Framework Effort is a powerful tool that enables a convenient way to create automated tests for Entity Framework based applications.
https://entityframework-effort.net/
MIT License
431 stars 99 forks source link

EffortException when BulkInserting a class with a nullable enum. #216

Closed alastairandrew closed 1 year ago

alastairandrew commented 1 year ago

EffortBugReport.zip

1. Description

When trying to use EntityFramework.BulkInsert method on a class that contains a nullable enum the insert fails due to a InvalidCastException. It appears as though the object's enum is being treated as its value name rather than the underlying int value during the insert somewhere.

2. Exception

Effort.Exceptions.EffortException: An unhandled exception occurred while trying to assign value 'Reason1' to Property 'Reason' of type 'System.Nullabl...

Effort.Exceptions.EffortException
An unhandled exception occurred while trying to assign value 'Reason1' to Property 'Reason' of type 'System.Nullable`1[System.Int32]' during entity initialization for table 'Effort.Internal.DbManagement.Schema.TableName'
   at Effort.Internal.DbManagement.Schema.DbTableInfo.HandleConvertException(Exception exception, PropertyInfo property, Object value)
   at lambda_method(Closure , Object[] )
   at .( , DbCommand )
   at .Execute(List`1 actions)
   at Z.BulkOperations.BulkOperation.Execute()
   at Z.EntityFramework.Extensions.EntityBulkOperation`1.BulkInsert()
   at .[](BulkOperation`1 this, DbContext , List`1 , Boolean , List`1 , Type , String )
   at .`1.(SchemaEntityType )
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at .[](DbContext this, BulkOperation`1 , IEnumerable`1 , List`1 )
   at DbContextExtensions.BulkInsert[T](DbContext this, IEnumerable`1 entities, Action`1 bulkOperationFactory)
   at EffortBugRepro.Models.CommentRepo.CopyComments(Int32 sourceInstance, Int32 targetInstance) in C:\Users\Alastair\RiderProjects\EffortBugReport\EffortBugRepro\Models\CommentRepo.cs:line 25
   at EffortBugRepro.Tests.Tests.ReproductionCase() in C:\Users\Alastair\RiderProjects\EffortBugReport\EffortBugRepro.Tests\Tests.cs:line 49

System.InvalidCastException
Specified cast is not valid.
   at lambda_method(Closure , Object[] )

3. Fiddle or Project

I've attached a minimal solution that reproduces the error I see in my actual project. The versions of the packages all match what we're using. There's a single test that populates some example data (with all three possible enum states) which it then attempts to "clone" (which is the feature we're writing and want to use Effort to unit test).

4. Any further technical details

Add any relevant detail can help us.

JonathanMagnan commented 1 year ago

Hello @alastairandrew ,

It looks like you didn't attach the project correctly. Your post doesn't look to contain it.

Best Regards,

Jon


Sponsorship Help us improve this library

Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsDapper Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval Function

alastairandrew commented 1 year ago

Hi @JonathanMagnan

Whoops, I've edited the original post and attached EffortBugReport.zip which is the solution. Sorry about that.

Cheers, AA

JonathanMagnan commented 1 year ago

Thank you @alastairandrew ,

We can successfully reproduce the issue with your project.

My developer will look at it. We already support enum so I believe it should not be a problem to support nullable enum.

Best Regards,

Jon

JonathanMagnan commented 1 year ago

Hello @alastairandrew ,

Just to let you know that the fix has been merged in our master.

The fix will be part of the next release of Z.EntityFramework.Extensions that should happen on 2023-09-12

I will let you know when the fix will be available

JonathanMagnan commented 1 year ago

Hello @alastairandrew ,

The v7.100.0.0 of Entity Framework Extensions has been released today.

Our library should now support nullable enum. Let us know if everything works as expected.

Best Regards,

Jon

alastairandrew commented 1 year ago

Hi @JonathanMagnan,

I've upgraded to the latest release and can confirm that the issue is resolved now. Thanks so much for the speedy turnaround and great comms.

Much appreciated, Alastair