自分のテーブルの1つが自分自身にマップされている状況があります。ある行(親)の主キーを他の行(子)への外部キーとして使用することができ、この外部キー列には、親を持たない行にはnullが含まれます。このような何か:null値を持つレコードの取得 - 流暢なnhibernate
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>();
}
}
マッピング:私はNULLが含まれている行を取得するには、問題を持つ(または言っています今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フィールドに追加します。私はこのようなヌルrepo.Get("ParentLocationType.LocationTypeId", null);
を渡ししようとしたが、それdidntの仕事が、object reference is not set to an instance error.
を照会するとき、私は私がやったそう
Expression.IsNull
代わりのExpression.Eq
を使用して、それを解決しましたそれはどちらかというと仕事はしませんでしたが、なぜ私は知っていると思います。「SELECT LocationId、Name、ParentLocationId FROM Settings_LocationType WHERE ParentLocationId = null」というsqlクエリを見てください。私はそれが "= null"を "null"と置き換える方法をいくつか解決すれば解決できると思います。どのようにこれを行うにはどのようなアイデア? – Waqas私はExpression.Eqの代わりにExpression.IsNullを使って解決しました – Waqas