简体   繁体   English

为什么 CreateSQLQuery 抛出异常“值 System.Object[] 不是类型,不能在此泛型集合中使用”?

[英]Why CreateSQLQuery throwing exception "The value System.Object[] is not of type and cannot be used in this generic collection"?

I have following database table:我有以下数据库表:

CREATE TABLE A
(
[Id]    [INT] IDENTITY (1,1) NOT NULL CONSTRAINT A_P_KEY PRIMARY KEY,
[X] [INT]
)

Following is entity class:以下是实体类:

public class A
{
    public virtual int Id { get; set; }
    public virtual int X { get; set; }
}

Following is mapping:以下是映射:

internal class AConfiguration : ClassMapping<A>
{
    public AConfiguration()
    {
        Table("A");

        Id(x => x.Id, im =>
        {
            im.Column("Id");
            im.Generator(Generators.Identity);
        });
        Property(x => x.X, map => map.NotNullable(true));
    }
}

Following is how I call the CreateSQLQuery :以下是我如何调用CreateSQLQuery

List<A> lst = null;
using(var session = SessionFactory.OpenSession())
{
    lst = session.QueryOver<A>().List<A>().ToList();//This works

    var sql = @"SELECT Id, X FROM A";
    lst = session.CreateSQLQuery(sql).List<A>().ToList();//This fail
}

As shown above, the QueryOver call works correctly.如上所示, QueryOver调用工作正常。 But the CreateSQLQuery call fails with following exception:但是CreateSQLQuery调用失败并出现以下异常:

System.ArgumentException was unhandled
  HResult=-2147024809
  Message=The value "System.Object[]" is not of type "[....].A" and cannot be used in this generic collection.
Parameter name: value
  ParamName=value
  Source=mscorlib
  StackTrace:
       at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType)
       at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item)
       at NHibernate.Util.ArrayHelper.AddAll(IList to, IList from)
       at NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery, QueryParameters queryParameters, IList results)
       at NHibernate.Impl.AbstractSessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results)
       at NHibernate.Impl.AbstractSessionImpl.List[T](NativeSQLQuerySpecification spec, QueryParameters queryParameters)
       at NHibernate.Impl.SqlQueryImpl.List[T]()
       at [....]
  InnerException: 

Why CreateSQLQuery call is throwing exception?为什么CreateSQLQuery调用抛出异常?

The answers for this question are not helpful as I am selecting all the columns from the table;这个问题的答案没有帮助,因为我正在选择表格中的所有列; that question talks about selecting limited columns.该问题涉及选择有限的列。

The CreateSQLQuery API will return object array System.Object[] . CreateSQLQuery API 将返回对象数组System.Object[] The exception is while attempting to map this object array with your entity.例外是尝试将此对象数组与您的实体映射时。

You have two ways:你有两种方法:

  1. Get object array and map manually手动获取对象数组并映射

    You can get the object array using session.CreateSQLQuery(sql).List<Object[]>() and map it further with your custom logic.您可以使用session.CreateSQLQuery(sql).List<Object[]>()获取对象数组,并使用您的自定义逻辑进一步映射它。 Refer to this or this resource for more details.有关更多详细信息,请参阅资源。

  2. Instruct NHibernate how to map it指示 NHibernate 如何映射它

    NHibernate have feature called Transformers . NHibernate 具有称为Transformers功能。 Transformers help mapping complex objects.转换器有助于映射复杂的对象。 You can use one something like below:您可以使用如下所示的内容:

     lst = session.CreateSQLQuery(sql) .SetResultTransformer(Transformers.AliasToBean<A>()) .List<A>() .ToList();

    Refer to this resource for more details.有关更多详细信息,请参阅资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:值“ System.Object []”不是类型“ prjCustomerService.clsCustomer”,因此不能在此通用集合中使用。 参数名称:值 - Error:The value “System.Object[]” is not of type “prjCustomerService.clsCustomer” and cannot be used in this generic collection. Parameter name: value 异常“类型&#39;System.Object []&#39;的对象不能转换为类型” - Exception “Object of type 'System.Object[]' cannot be converted to type” Linq Query 不断抛出“无法创建 System.Object 类型的常量值...”,为什么? - Linq Query keeps throwing “Unable to create a constant value of type System.Object…”, Why? 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型'System.Int32'的表达式不能用于返回类型'System.Object' - Expression of type 'System.Int32' cannot be used for return type 'System.Object' &#39;System.Int16&#39;类型的表达式不能用于返回类型&#39;System.Object&#39; - Expression of type 'System.Int16' cannot be used for return type 'System.Object' “System.Int64”类型的表达式不能用于返回类型“System.Object” - Expression of type 'System.Int64' cannot be used for return type 'System.Object' &#39;System.DateTime&#39;类型的表达式不能用于返回类型&#39;System.Object&#39; - Expression of type 'System.DateTime' cannot be used for return type 'System.Object' Linq和Equality Operator:类型'System.Int32'的表达式不能用于'System.Object'类型的参数 - Linq and the Equality Operator: Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM