代码之家  ›  专栏  ›  技术社区  ›  David Neale

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

  •  0
  • David Neale  · 技术社区  · 16 年前

    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; }...
    

    对NHibernate来说,最好的方法是什么?我现在使用的是S#arp,它利用Fluent NHibernate的自动映射功能。这会导致 Bookings 属性,因为它试图引用未映射的类( IBooking ).

    1 回复  |  直到 16 年前
        1
  •  1
  •   Richard    16 年前

    如果您使用的是fluent NHibernate,则可以这样做,它指定要将预订映射到的类名,而不是尝试映射到接口IBooking的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");
    
            }
    
    
        }
    
    推荐文章