检索具有空值的记录 - 流畅的 nhibernate
我遇到一种情况,我的一张表自行映射到自身。一行(父行)的主键可以用作另一行(子行)的外键,并且对于没有父行的此类行,该外键列包含 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(或者说不是子项)的那些行时遇到问题。我尝试像这样传递 null repo.Get("ParentLocationType.LocationTypeId", null);
但它不起作用,但引发了对象引用未设置为实例错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有没有尝试过:
Have you tried:
好的,在查询此类
LocationType
时,我使用Expression.IsNull
而不是Expression.Eq
解决了这个问题OK, I solved it using
Expression.IsNull
instead ofExpression.Eq
when querying for suchLocationType