【问题标题】:Dapper use singular table nameDapper 使用单数表名
【发布时间】:2015-08-25 13:04:34
【问题描述】:

我尝试了 Dapper 和 Dapper.Contrib。我有以下课程:

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

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

【问题讨论】:

标签: c# dapper


【解决方案1】:

在您的实体类上使用“表格”数据注释属性,如下所示:

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

【讨论】:

    【解决方案2】:

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

    另外,SqlMapperExtensions 上有一个名为 TableNameMapper 的静态委托。您可以将其替换为执行复数的实现。框架中的PluralizationService 可以在这里为您提供帮助。

    用法如下:

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

    【讨论】:

    • 我尝试使用该委托实现一个函数,但没有成功。您能否提供更多详细信息,并举例说明如何使用SqlMapperExtensions?特别是在// do something here to pluralize the name of the type部分,假设我们有POCO类Student,被Dapper复数后变成Student。另外,我创建了一个问题:link
    • 上面的文档链接已损坏。 specialized attributes的正确文档链接
    • @PBMe_HikeIt 已更新,感谢提醒!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多