Dapper use singular table name

南笙酒味 提交于 2020-05-23 08:03:25

问题


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 beeing mapped to the table "Customers" which is pluralized. Is there a simple way to make Dapper use singular table names for all tables?


回答1:


Dapper.Contrib supports the Table attribute. Use it to manually specify the name of the table that an entity uses. See the docs for further information.

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

It is used as follows:

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


来源:https://stackoverflow.com/questions/32204808/dapper-use-singular-table-name

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