我正在使用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);
}
}
我正在使用SQLiteasRDBMS并且我只保存Location, 依赖于Cascade()管理Places插入。
这两个实体已成功插入数据库中,但是当我尝试读取Location并访问其列表时Places,列表为空。
删除后Inverse(),代码似乎可以正常工作。但是当我检查数据库时,我在 中发现了两列Place table,而我只期望其中的一列:(Location_id那是空的)和LocationReference_id(那是设置的)。
经过一整天的绝望谷歌搜索,我注意到每个人都将引用命名property为类本身。所以,我把它从 重命名LocationReference为Location,我加回了Inverse()电话,一切正常。当然,只有列Location_id在数据库中。
有谁知道为什么会这样?谢谢。