如何让 Dapper 在映射时忽略/删除字段名称中的下划线?

2023-12-20

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

    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

有效的类:(精巧的已经忽略了大写)

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让 Dapper 在映射时忽略/删除字段名称中的下划线? 的相关文章

随机推荐