Store enum as string in database

后端 未结 3 757
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 22:59

I am experimenting with dapper. I have a class which has an enum and the values are stored as strings in the database.

This works with FluentNHibernate using GenericEnum

3条回答
  •  别那么骄傲
    2021-02-01 23:26

    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("select 'hello' as DBEnum")  <-- will set MyEnum
    

提交回复
热议问题