简体   繁体   中英

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 :

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. But the CreateSQLQuery call fails with following exception:

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?

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[] . 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. Refer to this or this resource for more details.

  2. Instruct NHibernate how to map it

    NHibernate have feature called 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM