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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With