![](/img/trans.png)
[英]NHibernate with mapping by code and a SQLite database: saving many-to-one parent-child entities, child gets a null foreign key
[英]NHibernate mapping many child entities to one entity
我目前有一个预订模型,例如:
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的最佳方法是什么? 我当前正在使用利用Fluent NHibernate的自动映射功能的S#arp。 由于尝试引用未映射的类( IBooking
),这给Bookings
属性带来了错误。
如果您使用的是流畅的NHibernate,则可以执行此操作,该操作指定要映射预订的类名,而不是Automapper尝试映射到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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.