Use a unique Dapper table name

I experimented with Dapper and Dapper.Contrib. I have the following class:

public class Customer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } public bool Active { get; set; } } 

It is tied to the Customers table, which is pluralized. Is there an easy way to force Dapper to use unique table names for all tables?

+8
c # dapper
source share
1 answer

Dapper.Contrib supports the Table attribute. Use it to manually specify the name of the table that the object uses. See docs for more details.

Alternatively, there is a static delegate to SqlMapperExtensions called TableNameMapper . You can replace this with an implementation that does pluralization. PluralizationService in a framework can help you here.

Used as follows:

 SqlMapperExtensions.TableNameMapper = (type) => { // do something here to pluralize the name of the type return type.Name; }; 
+11
source share

All Articles