本文介绍了NHibernate.Linq System.Nullable 抛出 ArgumentException,值“"不是类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MetadataRecord 类型的类:

I have a class of type MetadataRecord:

public class MetadataRecord {
    public virtual long? IntegerObject { get; set; }
    public virtual string ClassName { get; set; }
    public virtual DateTime? DateObject { get; set; }
    public virtual double? DecimalObject { get; set; }
    public virtual long MetadataId { get; set; }
    public virtual long MetadataLabelId { get; set; }
    public virtual long ObjectId { get; set; }
    public virtual string StringObject { get; set; }
    public virtual Asset Asset { get; set; }
}

和一个匹配的映射文件如下:

and a matching mapping file as follows:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="ActiveMediaDataAccess"
                   namespace="ActiveMediaDataAccess.Entities">

  <class name="MetadataRecord" table="WM_META_DATA" lazy="true">
    <id name="MetadataId" column="META_DATA_ID">
      <generator class="seqhilo" />
    </id>
    <property name="MetadataLabelId" column="META_DATA_LABEL_ID" />
    <property name="ObjectId" column="OBJECT_ID" />
    <property name="ClassName" column="CLASS_NAME" />
    <property name="IntegerObject" column="INTEGER_OBJECT" />
    <property name="DecimalObject" column="DECIMAL_OBJECT" />
    <property name="DateObject" column="DATE_OBJECT" />
    <property name="StringObject" column="STRING_OBJECT" />
    <many-to-one name="Asset" column="OBJECT_ID" not-null="true" />
  </class>
</hibernate-mapping>

我正在针对此类运行单元测试,以检查从 MetadataRecord 的实例为 IntegerObject 返回的值,该值是可空类型的 long.我使用 NHibernate.Linq (v 1.1.0.1001) 查询如下:

I'm running a unit test against this class to check for values returned for IntegerObject which is a nullable type of long, from an instance of MetadataRecord. I'm using NHibernate.Linq (v 1.1.0.1001) to query as follows:

[TestMethod()]
public void IntegerObjectTest() {
    var integerObject = _sessionFactory.OpenSession().Linq<MetadataRecord>()
                                       .Where(m => m.ObjectId == 65675L)
                                       .Select(m => m.IntegerObject)
                                       .FirstOrDefault();
    Assert.IsNull(integerObject);
}

相应表中的 INTEGER_OBJECT 列可以为 null,我希望 IsNull 为 true 或 false.但是,我收到以下错误:

The INTEGER_OBJECT column from the corresponding table is nullable, and I expect IsNull to be true or false. However, I get the following error:

测试方法 ActiveMediaMetadataViewerTestProject.MetadataRecordTest.IntegerObjectTest 抛出异常:NHibernate.Exceptions.GenericADOException:无法执行查找[SQL:SQL 不可用] ---> System.ArgumentException:值"不是System.Nullable"类型`1[System.Int64]" 并且不能在这个泛型集合中使用.参数名称:值.

我不明白为什么它试图将字符串转换为可空类型.是否有另一种方式我应该打开会话,装饰类,甚至构建映射文件,..... 我哪里出错了?我可以求助于使用 Criteria,但我非常享受 Linq 的智能感知和可重构性".

I can't figure out why it's trying to cast a string to a nullable type. Is there another way in which I should be opening the session, decorating the class, even constructing the mapping file, ..... where am I going wrong here? I could resort to using Criteria, but I was much enjoying the intellisense and "refactorability" with Linq.

推荐答案

更好的解决方案(整体翻译成 SQL):

Better solution (translated to SQL in whole):

[TestMethod()]
public void IntegerObjectTest() {
    var integerObject = _sessionFactory.OpenSession().Linq<MetadataRecord>()
                                       .Where(m => m.ObjectId == 65675L)
                                       .Select(m => new long?(m.IntegerObject))
                                       .FirstOrDefault();
    Assert.IsNull(integerObject);
}

这篇关于NHibernate.Linq System.Nullable 抛出 ArgumentException,值“"不是类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 00:38