34

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; }
}
scw
  • 5,450
  • 8
  • 36
  • 49
  • Are you sure you're using PostgreSQL and not SQL Server? – Andomar Dec 30 '15 at 19:43
  • 1
    Yes. :) Guessing you're asking because command type is "stored procedure" and postgresql just has functions? It still works. – scw Dec 30 '15 at 22:11

2 Answers2

99
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

job done ;p

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Wow. I put this in my connection factory and it works, but is there a better place? – scw Dec 30 '15 at 22:39
  • @scw well, you could write your own type map implementation... but: this works – Marc Gravell Dec 30 '15 at 22:53
  • 2
    Is there a way to do this conditionally for a single Dapper call? – julealgon Jul 21 '17 at 23:06
  • 1
    Additionally, this doesn't seem to work with constructor arguments. Was it intentional? Seems unintuitive/inconsistent to me that it would try to match members without underscores when this flag was set, but would not do the same for ctor parameters. The flag name talks about `names` in general so one would expect that all name-matching logic would be affected. – julealgon Jul 21 '17 at 23:15
  • @juleagon no, not intentional; I recommend logging a bug on github; edit: I see you already have - thanks – Marc Gravell Jul 22 '17 at 06:56
  • This only seems to work one way -- when selecting underscored columns from the DB into non-underscored model properties. But there is no way to insert/update those same underscored DB columns from the same non-underscored model properties. :( – htxryan Feb 02 '18 at 01:34
  • Worked perfect for me. What a time saver with hundred of tables using SNAKE_CASE. +1 – Yogi Mar 02 '20 at 10:58
  • 2
    For anyone looking at this thread then searching for the issue on GitHub about constructor parameters with underscores, here it is: https://github.com/DapperLib/Dapper/issues/818 However, as of right now there is still no solution. Altering the existing behaviour without adding a new config would break some known uses, including, apparently, Stack Overflow! – Josh Gallagher Oct 22 '21 at 07:33
  • Where should `Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;` be placed within an application? `Program.cs`? A repository class? – zwoolli Jul 26 '22 at 18:39
3

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.

Community
  • 1
  • 1
Andomar
  • 232,371
  • 49
  • 380
  • 404