IndexOutOfRange尝试使用nHibernate进行save()/ insert时出现异常

时间:2012-03-21 11:30:33

标签: hibernate nhibernate fluent-nhibernate fluent-nhibernate-mapping

当我从数据库中提取数据时,我有2个实体在一起玩得很好,但是现在我正在尝试保存一些东西,我得到一个奇怪的错误并且消除不能帮助我追踪原因:< / p>

enter image description here

预订

public class Booking
{
    public virtual int Id { get; set; }
    public virtual Int32 bookingID { get; set; }
}

预订位置

public class BookingLocation
{
    public virtual int Id { get; set; }
    public virtual Int32 bookingID { get; set; }
    public virtual Int32 locationID { get; set; } 
    public virtual DateTime startDateTime { get; set; }
    public virtual DateTime endDateTime { get; set; }
}

预订地图

public class BookingMap : ClassMap<Booking>
{
    public BookingMap()
    {
        Table("Bookings");

        Id(x => x.Id).Column("ID");
        Map(x => x.bookingID).Column("BookingID");

        HasMany(x => x.BookingLocations)                
           .KeyColumn("BookingID")
          .Not.LazyLoad().Cascade.All(); 
    }
}

BookLocation Mapping

public class BookingLocationMap : ClassMap<BookingLocation>
{
    public BookingLocationMap()
    {
        Table("Bookings_Locations");

        Id(x => x.Id).Column("ID");
        Map(x => x.bookingID).Column("BookingID");

        Map(x => x.locationID).Column("LID");
        Map(x => x.startDateTime).Column("startdatetime");
        Map(x => x.endDateTime).Column("enddatetime");


        References(x => x.Booking)
         .Column("BookingID")
         .Not.LazyLoad().Nullable()
         .Cascade.All();
    }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您正尝试将同一列映射两次(BookingId):

Map(x => x.bookingID).Column("BookingID");

References(x => x.Booking)
     .Column("BookingID")
     .Not.LazyLoad().Nullable()
     .Cascade.All();

如果您需要引用字段BookingId,则可以使用多对一(ReferencesBooking.BookingId,这样您的映射就会变为:

public class BookingLocationMap : ClassMap<BookingLocation>
{
    public BookingLocationMap()
    {
        Table("Bookings_Locations");

        Id(x => x.Id).Column("ID");
        Map(x => x.locationID).Column("LID");
        Map(x => x.startDateTime).Column("startdatetime");
        Map(x => x.endDateTime).Column("enddatetime");

        References(x => x.Booking)
         .Column("BookingID")
         .Not.LazyLoad().Nullable()
         .Cascade.All();
    }
}

实施例

我只读取了对我的馆藏的访问权限,因此我使用以下方法添加和删除此馆藏中的项目:

    public virtual void AddBookingLocation(BookingLocation bookingLocationToAdd)
    {
        bookingLocationToAdd.Booking = this;
        this.bookingLocations.Add(bookingLocationToAdd);
    }

    public virtual void RemoveBookingLocation(BookingLocation bookingLocationToRemove)
    {
        this.bookingLocations.Remove(bookingLocationToRemove);
    }