LINQ Entity Data Reader does not support enums

entity-framework enums sqlbulkcopy

Question

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?

1
5
11/15/2012 8:02:32 AM

Accepted Answer

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.

4
11/15/2012 8:02:32 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow