I'm using FastMember
to convert a List<T>
to a Datatable
. Some classes contain enums and this is causing problems when passing the datatables as a TVP to a stored procedure.
public class MyObject
{
public int Id {get; set;}
public SomeEnum EnumHere {get; set;}
}
var dt = new DataTable();
using (var reader = ObjectReader.Create(myObjectsList))
{
dt.Load(reader);
}
db.Execute<ResultObject>("insert_objects", new { dt }, commandType: CommandType.StoredProcedure);
FastMember
converts the list, however the column for the enum has a DataType
of SomeEnum
. When passing the datatable as a TVP, the following exception is thrown:
Exception thrown: 'System.ArgumentException' in Dapper.dll
Additional information: The type of column 'SomeEnum' is not supported. The type is 'SomeEnum'
Is there a way to force FastMember
to convert enums to int?
You can create an anonymous type to with the right properties:
var newObjects = from m in myObjectList
select new { m.Id, EnumHere = (int)m.EnumHere };
var dt = new DataTable();
using (var reader = ObjectReader.Create(newObjects))
{
dt.Load(reader);
}
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