检索具有空值的记录 - 流畅的nhibernate

时间:2010-12-08 10:57:48

标签: c# fluent-nhibernate

我有一种情况,我的一个表自我映射到自己。一行(Parent)的主键可以用作其他行(Child)的外键,并且此外键列对于没有父行的行包含null。像这样:

table: Settings_LocationType

++++++++++++++++++++++++++++++++++++++++++++++++
LocationID | Name     | ParentLocationId
++++++++++++++++++++++++++++++++++++++++++++++++
1            Parent 1   null
2            Child  1   1
3            Child  2   1
4            Parent 2   null

型号:LocationType

public class LocationType
{
        public virtual long LocationTypeId { get; set; }
        public virtual string Name { get; set; }
        public virtual LocationType ParentLocationType { get; set; }
        public virtual IList<LocationType> LocationTypes { get; set; }

        public LocationType()
        {
            LocationTypes = new List<LocationType>();         
        }
}

映射:LocationTypeMap

public class LocationTypeMap : ClassMap<LocationType>
    {
        public LocationTypeMap()
        {
            Table("Setting_LocationType");
            Id(x => x.LocationTypeId).Column("LocationId").GeneratedBy.Sequence("location_type_seq");
            Map(x => x.ShortName, "Name").Length(15).Not.Nullable();
            References<LocationType>(x => x.ParentLocationType).LazyLoad().Nullable();            
            HasMany<LocationType>(x => x.LocationTypes).AsBag().KeyColumn("ParentLocationId").KeyNullable().LazyLoad().Inverse().Cascade.SaveUpdate();
        }
    }

现在我在检索PatentLocationType字段中包含NULL(或者说不是子)的行时遇到问题。我尝试像repo.Get("ParentLocationType.LocationTypeId", null);一样传递null,但它没有用,只是扔了object reference is not set to an instance error.

2 个答案:

答案 0 :(得分:0)

你试过了吗?

repo.Get ("ParentLocationType", null)

答案 1 :(得分:0)

好的,我在查询Expression.IsNull

时使用Expression.Eq代替LocationType解决了问题