5

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?

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81

1 Answers1

4

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.

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • Why not just replace all instances of `IsScalarType` with `t.IsValueType` where `t` is the type to check? – Trisped Apr 25 '13 at 20:20
  • Structs are value types. Structs != scalar. – jnm2 Jul 19 '13 at 17:14
  • That's good, but not quite enough as it won't acknowledge nullable enum types as scalar types. I did some code to address that but somehow it still fails when bulk inserting. I'm still investigating. – Crono Sep 21 '17 at 22:36