LINQ Entity Data Reader does not support enumerations

I need to batch insert multiple objects, so I decided that the best way to do this is to use the SqlBulkCopy class. However, this class works with DataReader instances, while my code works with IEnumerable, where T is my entity class. To convert my IEnumerable to DataReader, I found the following code: LINQ Entity Data Reader .

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

+4
source share
1 answer

I found out that this is due to the IsScalarType method, which is not enumerated. This can be easily IsScalarType changing 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 that, the type of enumeration will also be recognized.

+3
source

Source: https://habr.com/ru/post/1446158/


All Articles