Dapper使用单表名称 [英] Dapper use singular table name

查看:304
本文介绍了Dapper使用单表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

它被映射到表 Customers 。有没有一种简单的方法可以使Dapper对所有表使用唯一的表名?

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?

推荐答案

Dapper.Contrib支持表格属性。使用它来手动指定实体使用的表的名称。有关更多信息,请参见 docs

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.

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

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.

它的用法如下:

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

这篇关于Dapper使用单表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆