我使用的是Fluent NHibernate
,我有两个entities
:
public class Location
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
private Stack<Place> _places;
public virtual IList<Place> Places
{
get { return _places.ToList(); }
set { _places = new Stack<Place>(value); }
}
public virtual void AddPlace(Place place)
{
_places.Push(place);
place.LocationReference = this;
}
}
public class Place
{
public virtual int Id { get; protected set; }
public virtual Location LocationReference { get; set; }
}
mapped
如下:
class LocationMap : ClassMap<Location>
{
public LocationMap()
{
Id(x => x.Id);
Map(x => x.Name)
.Not.Nullable()
.Unique();
HasMany(x => x.Places)
.Cascade.All()
.Inverse();
}
}
class PlaceMap : ClassMap<Place>
{
public PlaceMap()
{
Id(x => x.Id);
References(x => x.LocationReference);
}
}
我使用SQLite
作为RDBMS
而我只保存Location
,依靠Cascade()
来管理Places
插入。
这两个实体已成功插入数据库,但当我尝试读取Location
并访问其Places
列表时,列表为空。
删除Inverse()
后,代码似乎正常工作。但是当我查看数据库时,我在Place table
中找到了两列,而我只期望其中一列:Location_id
(空的)和LocationReference_id
(已设置)。< / p>
经过一整天绝望的谷歌搜索,我注意到每个人都将引用property
命名为类本身。所以,我将其从LocationReference
重命名为Location
,我添加了Inverse()
电话,一切正常。当然,只有列Location_id
在数据库中。
有谁知道为什么会这样?感谢。