有没有办法使用 Dapper 的某些 .Insert 扩展方法将属性映射到列名?

Is there a way to map properties to column names using some .Insert extension method for Dapper?

我对这个 class 有以下挑战:

Public Class MyClass
    Property Id As Integer
    Property LastName as String
End Class

数据库中对应的数据table有如下字段: Id (int, not null) Last-Name (nvarchar(80),null)

所以我需要将 MyClass.LastName 映射到 MyClasses.Last-Name 并且玩得很开心...

当我编写自定义插入查询时,一切正常,但我想使用 Dapper 扩展包之一的 .Insert 语句。

我尝试了 Dapper.Contrib,但这忽略了我使用 Dapper.FluentMap 或使用 Dapper 本身的内置方法使用 Dapper.SetTypeMap.

创建的映射

我试过 Dapper.FastCrud,但我无法弄清楚如何为其配置映射,尽管 API 看起来很有希望。

有人吗?

所以,基本上这里的问题是 属性 名称和列名称不同。

Dapper, you can handle this by providing alias for column name in SQL query. I guess you have already tried this as you said "I write a custom Insert query" in your question. Other multiple ways to map column names with properties are discussed here.

DapperExtensions, you can map 不同的列名称及其各自的属性如下所示:

public sealed class MyClassMapper : ClassMapper<MyClass>
{
    public MyClassMapper()
    {
        Table("MyTable");
        Map(x => x.Id).Key(KeyType.WhatYouWant);
        Map(x => x.LastName).Column("Last-Name");
        AutoMap();
    }
}

代码示例使用 C#。你必须把它翻译成 VB.NET.

通过 Dapper.Contrib, you can decorate the class with [Table] attribute to map the table name. Alternatively, you can use SqlMapperExtensions.TableNameMapper to map the tables. Please refer to this blog post or this and this and SO 帖子了解更多详情。
显然,有no way to map the column name. This feature is planned for next major (2.x) version. Version 2.x is released; but the issue is still open. Looking at the release notes,尚未添加功能。

Dapper.FastCrud, there are multiple ways for mapping。您可以使用 [Column] 属性修饰 属性。