[英]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:你有两种方法:
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.有关更多详细信息,请参阅此或此资源。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.