是否可以使用Dapper的某些.Insert扩展方法将属性映射到列名? [英] Is there a way to map properties to column names using some .Insert extension method for Dapper?

查看:556
本文介绍了是否可以使用Dapper的某些.Insert扩展方法将属性映射到列名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此类,我面临以下挑战:

I have the following challenge with this class:

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

数据库中相应的数据表具有as字段:
Id(int,not null)
姓氏(nvarchar(80),null)

The corresponding data table in the database has as fields: Id (int, not null) Last-Name (nvarchar(80),null)

所以我需要将 MyClass.LastName 映射到 MyClasses.Last-命名,好好玩...

So I need to map MyClass.LastName to MyClasses.Last-Name and have a hell of a time...

当我编写自定义插入查询时,所有方法都可行,但我想使用

When I write a custom Insert query it all works, but I would like to use the .Insert statement of one of the Dapper extensions packages.

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

I tried Dapper.Contrib, but this ignores mappings that I create using Dapper.FluentMap or using the built in method of Dapper itself using Dapper.SetTypeMap.

我尝试了Dapper.FastCrud,但我无法弄清楚可以为此配置映射,尽管该API似乎很有希望。

I tried Dapper.FastCrud, but I was unable to figure out how to configure mappings for it, though this API seems promising.

任何一个?

推荐答案

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

So, basically the problem here is that the property name and column name is different.

使用 Dapper ,您可以处理通过在SQL查询中为列名提供别名来实现。我想您已经在您的问题中说 我编写了自定义插入查询时就已经尝试过了。在此处中讨论了其他使用属性映射列名称的方法。

With 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 ,您可以映射不同的列名及其各自的属性,如下所示:

With DapperExtensions, you can map the different column names with their respective properties something like below:

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 ,您可以使用 [Table] 属性以映射表名称。或者,您可以使用 SqlMapperExtensions.TableNameMapper 映射表。请参阅博客文章或 SO帖子以获取更多详细信息。

显然,有没办法映射列名称。计划在下一个主要(2.x)版本中使用此功能。

With 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 this SO posts for more details.
Apparently, there is no way to map the column name. This feature is planned for next major (2.x) version.

使用 Dapper.FastCrud ,其中有多种方式用于映射。您可以使用 [Column] 属性装饰该属性。

With Dapper.FastCrud, there are multiple ways for mapping. You can decorate the property with [Column] attribute.

这篇关于是否可以使用Dapper的某些.Insert扩展方法将属性映射到列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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