Dapper 使用单数 table 名称

Dapper use singular table name

我尝试了 Dapper 和 Dapper.Contrib。我有以下 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; }
}

它被映射到复数形式的 table“Customers”。有没有一种简单的方法可以让 Dapper 对所有 table 使用单数 table 名称?

Dapper.Contrib 支持 Table 属性。使用它来手动指定实体使用的 table 的名称。有关详细信息,请参阅 docs

或者,框架中 SqlMapperExtensions 上有一个名为 TableNameMapper. You can replace this with an implementation that performs the pluralization. PluralizationService 的静态委托可以帮助您。

用法如下:

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

在您的实体 class 上使用 'Table' 数据注释属性,如下所示:

[Table("Customer")]
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; }
}