开发者

Retrieving records with null value - fluent nhibernate

开发者_如何学运维

I have a situation where one of my table is self-mapped to itself. The primary key of one row (Parent) can be used as a foreign key to other row (Child) and this foreign key column contains null for such rows that have no parent. Something like this:

table: Settings_LocationType

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

Model: 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>();         
        }
}

Mapping: 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();
        }
    }

Now I am having a problem in retrieving those rows which contain NULL (or say aren't child) in PatentLocationType field. I tried passing null like this repo.Get("ParentLocationType.LocationTypeId", null); but it didnt work but threw object reference is not set to an instance error.


Have you tried:

repo.Get ("ParentLocationType", null)


OK, I solved it using Expression.IsNull instead of Expression.Eq when querying for such LocationType

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜