流畅的NHibernate ManyToOne参考属性名称问题

时间:2013-07-17 18:47:15

标签: c# fluent-nhibernate one-to-many fluent-nhibernate-mapping

我使用的是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在数据库中。

有谁知道为什么会这样?感谢。

0 个答案:

没有答案