服务器作为外键(正如您所说),而不是预订(我想我被搞糊涂了)。令人惊讶的是,nHibernate(和你)能很好地理解我的无意义;)感谢nHibernate探查器提示!你是个传奇人物,乔!非常感谢你的帮助!我现在要试一试……是的,我也处在这种情况下,这不起作

服务器作为外键(正如您所说),而不是预订(我想我被搞糊涂了)。令人惊讶的是,nHibernate(和你)能很好地理解我的无意义;)感谢nHibernate探查器提示!你是个传奇人物,乔!非常感谢你的帮助!我现在要试一试……是的,我也处在这种情况下,这不起作,nhibernate,fluent-nhibernate,nhibernate-mapping,fluent-nhibernate-mapping,Nhibernate,Fluent Nhibernate,Nhibernate Mapping,Fluent Nhibernate Mapping,服务器作为外键(正如您所说),而不是预订(我想我被搞糊涂了)。令人惊讶的是,nHibernate(和你)能很好地理解我的无意义;)感谢nHibernate探查器提示!你是个传奇人物,乔!非常感谢你的帮助!我现在要试一试……是的,我也处在这种情况下,这不起作用。奇怪的是,这似乎正是问题所在,在这之前我有XML映射,它做了完全相同的事情(据我所见),但没有得到这个错误 public class Booking { public virtual int Id { get; set; }


服务器作为外键(正如您所说),而不是预订(我想我被搞糊涂了)。令人惊讶的是,nHibernate(和你)能很好地理解我的无意义;)感谢nHibernate探查器提示!你是个传奇人物,乔!非常感谢你的帮助!我现在要试一试……是的,我也处在这种情况下,这不起作用。奇怪的是,这似乎正是问题所在,在这之前我有XML映射,它做了完全相同的事情(据我所见),但没有得到这个错误
public class Booking
{
    public virtual int Id { get; set; }

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

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

    public virtual IList<BookingLocation> BookingLocations { get; set; }

    public Booking()
    {
        BookingLocations = new List<BookingLocation>();
    }
}

public BookingMap()
{
    Table("Bookings");

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

public class BookingLocation
{
    public virtual int Id { get; set; }
    public virtual Booking Booking { get; set; }
}

public BookingLocationMap()
{
    Table("Bookings_Locations");    

    Id(x => x.Id).Column("ID");

    References(x => x.Booking)             
      .Column("ID")             
      .Not.LazyLoad().Nullable()
      .Cascade.All();
}
Booking.AddBookingLocation(BookingLocation);
this.bookingRepo.insertBooking(Booking, BookingLocation); 
public class BookingLocation
{
        public BookingLocation(Booking inBooking)
        {
            this.Booking = inBooking;
            inBooking.AddBookingLocation(this);
        } 
}
public BookingLocationMap()
{
    Table("Bookings_Locations");    

    Id(x => x.Id).Column("ID").GeneratedBy.Identity(); // Try this


    References(x => x.Booking)             
      .Column("ID")             
      .Not.LazyLoad().Nullable()
      .Cascade.All();
}
HasMany(x => x.BookingLocations)
   .KeyColumn("BookingID") // aka, column in BookingLocation that links a row to this Booking
   .Not.LazyLoad()
   //.Cascade.All() // would advise against this until you can get it working explicitly
   .Inverse() // use this instead
   ;
References(x => x.Booking)             
   .Column("BookingID") // the column in the locations table that points to the booking.
   .Not.LazyLoad()
   ;
using (var session = CreateNewSession())
{
    using (var trans = session.BeginTransaction())
    {
        session.Save(myNewBooking)
        myNewBooking.BookingLocations.ForEach(x => session.Save(x));
        trans.Commit();
    }
}
Bookings (table) ID Booking_Locations (table) ID BookingID Booking (class) ID IList<BookingLocation> BookingLocation (class) ID Booking (instance)
base.HasMany<BookingLocation>(x => x.BookingLocations)
.KeyColumn("BookingID")
.Inverse()
.Not.LazyLoad()
;
base.References<Booking>(x => x.Booking)
.Column("BookingID")
.Not.Nullable()
;
var newBooking = new Booking(); // new booking
var newBLocation = new BookingLocation(); // new booking location
newBLocation.Booking = newBooking; // set booking instance on booking location
newBooking.BookingLocations.Add(newBLocation); // add new booking location to booking

session.Save(newBooking); // get the newBooking's ID from the DB insert
session.Save(newBLocation); // save the new location. NHibernate will set the FK col BookingID to the ID of the newly-persisted newBooking, and also get the new ID for newBLocation