映射时,如何使Dapper忽略/删除字段名称中的下划线? [英] How to get Dapper to ignore/remove underscores in field names when mapping?

查看:877
本文介绍了映射时,如何使Dapper忽略/删除字段名称中的下划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多方法可以将数据库字段名映射到类名,但是删除下划线的最简单方法是什么?

There are many ways to map database field names to class names, but what is the simplest way to just remove the underscores?

    public IEnumerable<PersonResult> GetPerson(int personId)
    {
        using (var dbConnection = _dbConnectionFactory.Create(ConnectionStrings.ProjectXYZ))
        {
            IEnumerable<PersonResult> result =
                dbConnection.Query<PersonResult>("fn_get_person", new { personId },
                    commandType: CommandType.StoredProcedure).ToList();

            return result;
        }
    }

表和数据库字段:

person
-------- 
person_id
full_name

有效的类:(精简程序已经忽略了大写字母)

Class that works: (dapper already ignores capitalization)

public class PersonResult
{    
    public int Person_Id { get; set; }
    public string Full_Name { get; set; }
}

我想将类更改为:

public class PersonResult
{    
    public int PersonId { get; set; }
    public string FullName { get; set; }
}

推荐答案

Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

工作完成; p

这篇关于映射时,如何使Dapper忽略/删除字段名称中的下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆