Fluent Nhibernate:如何映射双向关系

时间:2012-12-11 00:43:15

标签: c# nhibernate fluent-nhibernate fluent-nhibernate-mapping bidirectional-relation

我正在尝试映射以下内容但不确定它是否完全可以使用当前的nHibernate / fluent nHibernate,所以任何建议都会非常感激。

基本上我有以下两个班级

public class Venue
{
    public virtual int ID {get;set;}
    public virtual VenueDetail CurrentDetails {get; set;}
    public virtual IEnumerable<VenueDetail> PreviousDetails {get; set;}
}

public class VenueDetail
{
    public virtual int ID {get;set;}
    public virtual string Description {get;set;}
    public virtual Venue Venue {get;set;}
}

有没有办法有效地映射上述关系?我的Venue表有一个VenueDetail的外键用于CurrentVenueDetails属性,同时我的VenueDetail还有一个外键返回Venue属性的Venue表。

干杯

1 个答案:

答案 0 :(得分:0)

It is pretty much all described here for you.

public class VenueDetailMap : ClassMap<VenueDetail>
{
    ....
    //Reference the One side of the OneToMany
    Reference(x => x.Venue);
    ....
}

public class VenueMap : ClassMap<Venue>
{
    ....
    //Declare this is the many side
    HasMany(x => x.PreviousDetails);
    // We are referencing another entity here
    References( x => x.CurrentDetails);
    ....
}