当结果集具有未映射的列时,如何使Dapper.NET抛出? [英] How can I make Dapper.NET throw when result set has unmapped columns?

查看:66
本文介绍了当结果集具有未映射的列时,如何使Dapper.NET抛出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的示例代码作为上下文...当我运行此查询时,我得到的'Id'字段将作为默认值返回(对于int而言为0)。我想告诉dapper以某种方式运行,如果结果集中有一列未映射到我的结果对象上的属性,它将引发异常。 (我知道问题只是我需要在SQL查询中删除多余的'd',但我有兴趣让它更明确地公开自身)

Using the example code below as context... When I run this query I get the 'Id' field coming back as default value (which is 0 for an int). I would like to tell dapper to run in a manner where it would throw an exception if there is a column in the result set that does not get mapped to a property on my result object. (I understand that the issue is just that I need to remove the extra 'd' in the SQL query but I'm interested in having this expose itself more explicitly)

我一直找不到有关此主题的任何内容。

I've been unable to find anything on this topic. Please let me know if this is even possible with Dapper.

在此先感谢您(除了此问题,对于尚未尝试的人,Dapper确实是

Thanks in advance (besides this issue, and for anyone who hasn't taken the plunge, Dapper really is the greatest thing since sliced bread!).

class CustomerRecord
{
    public int Id { get; set; }
    public string Name { get; set; }
}

CustomerRecord[] GetCustomerRecords()
{
    CustomerRecord[] ret;
    var sql = @"SELECT 
                 CustomerRecordId AS Idd, 
                 CustomerName as Name
                 FROM CustomerRecord";

    using (var connection = new SqlConnection(this.connectionString))
    {
        ret = connection.Query<CustomerRecord>(sql).ToArray();
    }

    return ret;
}


推荐答案

您可以创建自己的类型在使用Dapper的 DefaultTypeMap 的位置映射,并在找不到成员时引发异常:

You could create your own type map where you use Dapper's DefaultTypeMap and throw an exception when it cannot find the member:

public class ThrowWhenNullTypeMap<T> : SqlMapper.ITypeMap
{
    private readonly SqlMapper.ITypeMap _defaultTypeMap = new DefaultTypeMap(typeof(T));

    public ConstructorInfo FindConstructor(string[] names, Type[] types)
    {
        return _defaultTypeMap.FindConstructor(names, types);
    }

    public ConstructorInfo FindExplicitConstructor()
    {
        return _defaultTypeMap.FindExplicitConstructor();
    }

    public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
    {
        return _defaultTypeMap.GetConstructorParameter(constructor, columnName);
    }

    public SqlMapper.IMemberMap GetMember(string columnName)
    {
        var member = _defaultTypeMap.GetMember(columnName);
        if (member == null)
        {
            throw new Exception();
        }
        return member;
    }
}

此方法的缺点是,您必须配置所有每个实体的类型映射:

Downside of this, is that you have to configure all the type maps for every entity:

SqlMapper.SetTypeMap(typeof(CustomerRecord), typeof(ThrowWhenNullTypeMap<CustomerRecord>));

但这可以使用反射进行配置。

This could be configured using reflection, however.

这篇关于当结果集具有未映射的列时,如何使Dapper.NET抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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