Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Entity Data Reader does not support enums

I have to do a batch insert of a lot of entities, so I figured the best way to do that was to use the SqlBulkCopy class. However, that class operates on DataReader instances, whereas my code works with an IEnumerable where T is my entity class. To convert my IEnumerable to a DataReader, I found the following code: LINQ Entity Data Reader.

This code works fine, but there is one problem: enum properties on my entity type are not included in the datareader (and therefore not being inserted correctly). How can I have the enum type properties be recognized?

like image 644
Erik Schierboom Avatar asked Oct 05 '22 23:10

Erik Schierboom


1 Answers

I found out this is due to the IsScalarType method not taking into account enums. This can easily be fixed by modifying the IsScalarType method as follows:

private static bool IsScalarType(Type t)
{
    // The || t.IsEnum part is new and makes sure that enums are recognized
    return scalarTypes.Contains(t) || t.IsEnum;
}

After this modification enum type will also be recognized.

like image 81
Erik Schierboom Avatar answered Oct 10 '22 03:10

Erik Schierboom