NHibernate将许多子实体映射到一个实体

时间:2010-08-20 08:31:26

标签: c# nhibernate fluent-nhibernate domain-driven-design nhibernate-mapping

我目前有一个预订模型,例如:

public class RoomBooking : Entity, IBooking
{
    public virtual Client Client { get; set; }
    public virtual Address Address { get; set; }
    public virtual IList<BookingPeriod> BookingPeriods{get;set;}...


public class BookingPeriod : Entity
{
    public virtual IEnumerable<IBooking> Bookings { get; set; }
    public virtual DateTime StartTime { get; set; }
    public virtual DateTime EndTime { get; set; }...

预订与BookingPeriods有很多关系。可以制作三种不同类型的预订,每种预订都有相关的类型。

为NHibernate映射这个的最佳方法是什么?我目前正在使用S#arp,它使用Fluent NHibernate的AutoMapping功能。这会在Bookings属性上产生错误,因为它试图引用未映射的类(IBooking)。

1 个答案:

答案 0 :(得分:1)

如果你使用流畅的NHibernate,你可以这样做,它指定你正在映射预订的类名,而不是试图映射到界面的Automapper:

 public class RoomBooking : Entity, IBooking
{
    public virtual Client Client { get; set; }
    public virtual Address Address { get; set; }
    public virtual IList<BookingPeriod> BookingPeriods { get; set; }

}

public class RoomBookingMap : EntityMap<RoomBooking>
{
    public RoomBookingMap()
    {
        Map(x => x.Client);
        Map(x => x.Address);
        HasManyToMany(x => x.BookingPeriods);
    }

}

public class BookingPeriod : Entity
{
    public virtual IEnumerable<IBooking> Bookings { get; set; }
    public virtual DateTime StartTime { get; set; }
    public virtual DateTime EndTime { get; set; }
}

public class BookingPeriodgMap : EntityMap<BookingPeriod>
{
    public BookingPeriodgMap()
    {
        Map(x => x.StartTime);
        Map(x => x.EndTime);
        HasManyToMany<RoomBooking>(x => x.Bookings);
    }

}
 public class EntityMap<T> : ClassMap<T> where T : Entity
    {
        public EntityMap()
        {
            Id(x => x.Id)
                .UnsavedValue(-1)
                .Column("id");

        }


    }