【发布时间】:2014-12-11 17:58:04
【问题描述】:
我有 2 个 NHibernate 实体(表),它们是一对多关系。
我收到此错误:
无法为表中的标识列插入显式值 IDENTITY_INSERT 设置为 OFF 时的“Bookings_Locations”。
尝试保存(插入)新记录时。
实体
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();
}
BookingLocation 的主键 (ID) 的值在初始化后立即为 0,我没有为其分配任何内容。这让我相信问题出在关系映射中。
我在调用截图中的方法之前这样做:
Booking.AddBookingLocation(BookingLocation);
this.bookingRepo.insertBooking(Booking, BookingLocation);
我以前没有做过这样的关系,所以可能会很远......
任何帮助表示赞赏:)
编辑:
我现在有一个错误:
“对象引用未设置为对象的实例。”
...当SessionFactory.OpenSession 运行时。
这有点关系:
public class BookingLocation
{
public BookingLocation(Booking inBooking)
{
this.Booking = inBooking;
inBooking.AddBookingLocation(this);
}
}
...因为当我删除它运行的构造函数时(考虑不添加BookingLocation)
我猜在它打开数据库时,根据您的示例,没有实例被传递到构造函数中。你懂我的意思吗?
【问题讨论】:
标签: nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping