Can I specify DB column names for dapper-dot-net mappings?

后端 未结 4 540
别跟我提以往
别跟我提以往 2021-02-05 05:04

Is there a way with dapper-dot-net to use an attribute to specify column names that should be used and not the property name?

public class Code
{
    public int          


        
4条回答
  •  鱼传尺愫
    2021-02-05 05:55

    Another approach is to just manually map it with the dynamic result.

    var codes = conn.Query(...sql and params here...)
                     .Select(s=>new Code{Id = s.Id, Type = s.Type, Value = s.code, Description = s.Description});
    

    Clearly this introduces type-safety scenarios because you are querying on dynamic. Also, you have to manually map columns which is a bummer.

    However, I tend to like this approach because it's so darned transparent. You can cast if need be (as is the case with Enums), and basically just do whatever it is you need to do to go from the db recordset to your properties.

提交回复
热议问题