How to get Dapper to ignore/remove underscores in field names when mapping?

空扰寡人 提交于 2019-12-03 08:26:56

问题


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;
        }
    }

Table and database fields:

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; }
}

What I would like to change the class to:

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

回答1:


Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

job done ;p




回答2:


The readme doesn't show any support for renaming columns. You could alias them in select:

select person_id as personid, full_name as fullname from fn_get_person();

as suggested in this answer.



来源:https://stackoverflow.com/questions/34533349/how-to-get-dapper-to-ignore-remove-underscores-in-field-names-when-mapping

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