如何在名为IQuery的NHibernate上设置C#可为空的值类型值? [英] How can C# nullable value typed values be set on NHibernate named IQuery parameters?

查看:87
本文介绍了如何在名为IQuery的NHibernate上设置C#可为空的值类型值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NHibernate并通过命名查询调用存储过程:

I am using NHibernate and calling a stored procedure via a named query:

<sql-query name="SearchStuff" read-only="true" cacheable="true">
  <return class="ResultEntity" />
  EXEC [SearchStuff] ?, ?, ?    </sql-query>

许多存储过程参数故意为空-无法更改.

Many of the stored procedure parameters are deliberately nullable - this cannot be changed.

C#:

IQuery listQuery = this.Session.GetNamedQuery("SearchStuff");
listQuery.SetInt32(0, param1);
listQuery.SetDateTime(1, param2);
listQuery.SetString(2, param3);
IList<ResultEntity> results = listQuery.List<ResultEntity>();

不幸的是,NHibernate没有为可空值类型提供任何SetXyz()方法,因此我尝试添加一些扩展方法来进行补偿:

Unfortunately, NHibernate does not provide any SetXyz() methods for nullable value types so I tried adding some extension methods to compensate:

public static class QueryExtensions
{
    public static void SetInt32(this IQuery query, int position, int? val)
    {
        if (val.HasValue)
        {
            query.SetInt32(position, val.Value);
        }
        else
        {
            query.SetParameter(position, null);
        }
    }

    public static void SetInt32(this IQuery query, string name, int? val)
    {
        if (val.HasValue)
        {
            query.SetInt32(name, val.Value);
        }
        else
        {
            query.SetParameter(name, null);
        }
    }

    public static void SetDateTime(this IQuery query, int position, DateTime? val)
    {
        if (val.HasValue)
        {
            query.SetDateTime(position, val.Value);
        }
        else
        {
            query.SetParameter(position, null);
        }
    }

    public static void SetDateTime(this IQuery query, string name, DateTime? val)
    {
        if (val.HasValue)
        {
            query.SetDateTime(name, val.Value);
        }
        else
        {
            query.SetParameter(name, null);
        }
    }
}

我尝试了这些版本的各种版本,但是没有用.上面的代码因错误而失败:

I've tried various versions of these but none work. The code above fails with the error:

System.ArgumentNullException : A type specific Set(position, val) should be called because the Type can not be guessed from a null value.

我也尝试过不设置参数,但是NHibernate要求设置每个参数.我尝试过使用位置版本和命名版本都具有相同的结果.

I also tried simply not setting the parameter but NHibernate requires every parameter to be set. I've tried using both positional and named versions with the same results.

在NHibernate命名查询中,是否可以将空值分配给值类型的参数?

Is there any way to assign null values to value typed parameters in NHibernate named queries?

推荐答案

好的,事实证明SetParameter上有一些重写,可以显式设置类型.例如:

OK, it turns out there are some overrides on SetParameter that allow the type to be set explicitly. For example:

query.SetParameter(position, null, NHibernateUtil.Int32);

完整的扩展方法(仅适用于Int32和DateTime)现在为:

The full extension methods (for Int32 and DateTime only) are now:

public static class QueryExtensions
{
    public static void SetInt32(this IQuery query, int position, int? val)
    {
        if (val.HasValue)
        {
            query.SetInt32(position, val.Value);
        }
        else
        {
            query.SetParameter(position, null, NHibernateUtil.Int32);
        }
    }

    public static void SetInt32(this IQuery query, string name, int? val)
    {
        if (val.HasValue)
        {
            query.SetInt32(name, val.Value);
        }
        else
        {
            query.SetParameter(name, null, NHibernateUtil.Int32);
        }
    }

    public static void SetDateTime(this IQuery query, int position, DateTime? val)
    {
        if (val.HasValue)
        {
            query.SetDateTime(position, val.Value);
        }
        else
        {
            query.SetParameter(position, null, NHibernateUtil.DateTime);
        }
    }

    public static void SetDateTime(this IQuery query, string name, DateTime? val)
    {
        if (val.HasValue)
        {
            query.SetDateTime(name, val.Value);
        }
        else
        {
            query.SetParameter(name, null, NHibernateUtil.DateTime);
        }
    }
}

这篇关于如何在名为IQuery的NHibernate上设置C#可为空的值类型值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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