NHibernate 之 “ 值“System.Object[]”不是“”类型,不能在此泛型集合中使用。 参数名: value”

本文介绍了一个关于NHibernate框架中执行分页查询时遇到的问题及解决方案。具体问题是在使用NHibernate的CreateSQLQuery方法执行分页查询时,返回的数据类型为System.Object[]而非预期的OutStorageDataView类型。文中提供了修正后的代码示例,通过设置查询返回的具体类型解决了该问题。

        public IList<OutStorageDataView> GetItemList(int Brand_ID, int pageNo, int pageSize, out int TotalNumber)
        {                       
            IList<OutStorageDataView> List;
            ICriteria critRecordCount = NHibernateSessionReader.CreateCriteria(typeof(OutStorageDataView));          
            //查询总数(未入数据)
            TotalNumber = Convert.ToInt32(critRecordCount.SetProjection(Projections.Count("Item_ID")).UniqueResult());
            ////获得当前页的数据
            //ICriteria crit = NHibernateSessionReader.CreateCriteria(typeof(OutStorageDataView));
            //crit.Add(Restrictions.Eq("Brand_ID", Brand_ID));
            //crit.SetFirstResult((pageNo - 1) * pageSize)
            //                   .SetMaxResults(pageSize);
            //List = crit.List<OutStorageDataView>();
            //return List;
            //------------
            IQuery crit = NHibernateSessionReader.CreateSQLQuery("SELECT [Item_ID],[Item_Title],[Item_URL],[SearchEngine_Name],[Task_KeyWord],[Brand_Name],[Item_SDate],[Item_CDate],[Admin_Name],[Brand_ID] FROM [OutStorageDataView]");
            
            //crit.Add(Restrictions.Eq("Brand_ID", Brand_ID));
            crit.SetFirstResult((pageNo - 1) * pageSize)
                               .SetMaxResults(pageSize);
            //List = (List<OutStorageDataView>)crit.List<OutStorageDataView>();
            List = crit.List<OutStorageDataView>();
            return List;
        }

        #endregion
    }


报错:

值“System.Object[]”不是“IWOMTracker.Data.Model.OutStorageDataView”类型,不能在此泛型集合中使用。

参数名: value

修正:

List = crit.List<OutStorageDataView>();
因为默认返回的数值类型是System.Object[] 不不是“IWOMTracker.Data.Model.OutStorageDataView”类型

改成:

IQuery crit = NHibernateSessionReader.CreateSQLQuery("SELECT [Item_ID],[Item_Title],[Item_URL],[SearchEngine_Name],[Task_KeyWord],[Brand_Name],[Item_SDate],[Item_CDate],[Admin_Name],[Brand_ID] FROM [OutStorageDataView]", "t", typeof(OutStorageDataView));
            //crit.Add(Restrictions.Eq("Brand_ID", Brand_ID));
            crit.SetFirstResult((pageNo - 1) * pageSize)
                               .SetMaxResults(pageSize);
            List = (List<OutStorageDataView>)(crit.List<OutStorageDataView>());
            return List;

进行强类型转换,就OK了。

NHibernate.Cfg.HibernateConfigException HResult=0x80131500 Message=An exception occurred parsing configuration :The 'name' attribute is invalid - The value 'command_interceptor' is invalid according to its datatype 'Union' - The value 'command_interceptor' is not valid according to any of the memberTypes of the union. Source=NHibernate StackTrace:NHibernate.Cfg.XmlSchemas.ConfigSettingsValidationEventHandler(Object sender, ValidationEventArgs e) 在 System.Xml.Schema.XmlSchemaValidator.SendValidationEvent(XmlSchemaValidationException e, XmlSeverityType severity) 在 System.Xml.Schema.XmlSchemaValidator.SendValidationEvent(String code, String[] args, Exception innerException) 在 System.Xml.Schema.XmlSchemaValidator.CheckAttributeValue(Object value, SchemaAttDef attdef) 在 System.Xml.Schema.XmlSchemaValidator.ValidateAttribute(String localName, String namespaceUri, XmlValueGetter attributeValueGetter, String attributeStringValue, XmlSchemaInfo schemaInfo) 在 System.Xml.XsdValidatingReader.ValidateAttributes() 在 System.Xml.XsdValidatingReader.ProcessElementEvent() 在 System.Xml.XsdValidatingReader.ProcessReaderEvent() 在 System.Xml.XsdValidatingReader.Read() 在 System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space) 在 System.Xml.XPath.XPathDocument..ctor(XmlReader reader, XmlSpace space) 在 NHibernate.Cfg.ConfigurationSchema.HibernateConfiguration..ctor(XmlReader hbConfigurationReader, Boolean fromAppSetting) 在 NHibernate.Cfg.Configuration.Configure(XmlReader textReader) 在 NHibernate.Cfg.Configuration.Configure(String fileName) 在 Nebula.EAP.Core.Scenario.Dao.Session.ReadWriteSessionManager.BuildSessionFactory(String key) 在 F:\ProgramNew\nebula.eap.core\Nebula.EAP.Core.Scenario\Dao\Session\ReadWriteSessionManager.cs 中: 第 75 行 在 Nebula.EAP.Core.Scenario.Dao.Session.ReadWriteSessionManager.<>c.<.cctor>b__3_0() 在 F:\ProgramNew\nebula.eap.core\Nebula.EAP.Core.Scenario\Dao\Session\ReadWriteSessionManager.cs 中: 第 16 行 在 System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode) 内部异常 1: XmlSchemaValidationException: The 'name' attribute is invalid - The value 'command_interceptor' is invalid according to its datatype 'Union' - The value 'command_interceptor' is not valid according to any of the memberTypes of the union. 内部异常 2: XmlSchemaException: The value 'command_interceptor' is not valid according to any of the memberTypes of the union.
06-24
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值