NHibernate mapping many child entities to one entity
I currently have a bookings model such as:
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; }...
Bookings have a many to many relationship with BookingPeriods. There are three different types of bookings which can be made and each of them have an associated开发者_如何学Go type.
What would be the best way to map this for NHibernate? I'm currently using S#arp which utilises Fluent NHibernate's AutoMapping functionality. This gives the error on the Bookings
property as it is trying to reference an unmapped class (IBooking
).
If you are using fluent NHibernate you can do this, which specifies the class name you are mapping the bookings to rather than the Automapper trying to map to the interface IBooking:
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");
}
}
精彩评论