How to convert enum to int automatically via FastMember?

I use FastMember to convert a List<T> to Datatable . Some classes contain enumerations, and this causes problems when transferring data in the form of 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 to enumerate has a DataType of SomeEnum . When transmitting data such as TVP, the following exception is thrown:

Exception thrown: "System.ArgumentException" in Dapper.dll

Additional Information: The column type "SomeEnum" is not supported. Type "SomeEnum"

Is there a way to get FastMember convert enums to int?

+5
source share
1 answer

You can create an anonymous type with the desired 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); } 
+1
source

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


All Articles