Store enum as string in database

霸气de小男生 提交于 2019-12-02 20:34:17
Sam Saffron

This is not built in at the moment, there is a proposed solution for this here: http://code.google.com/p/dapper-dot-net/issues/detail?id=24 which we are yet to decide on. I like the idea of extensible type converters

As it stands the cleanest way to do this would be to define shadow property eg:

class MyType
{
   public MyEnum MyEnum {get; private set;}
   private string DBEnum { set { MyEnum = Convert(value);} }

   private MyEnum Convert(string val)
   {
     // TODO: Write me 
   } 
}

// cnn.Query<MyType>("select 'hello' as DBEnum")  <-- will set MyEnum

From the sample code I've just tried, it appears not. You can map an enum to its underlying integer value, but if you try to map it to its string value, a DataException is thrown.

Clay

Another solution could be to use the new ITypeMap extensability although it is still a bit verbose to implement.

https://stackoverflow.com/a/12615036/444917

Map a Uri field using Dapper

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!