Fluent NHibernate:初始化 OneToMany 集合
我在两个对象(Person 和 Order)之间有标准的一对多关系。这在我的对象模型中表示为 Person 上的订单集合。这在数据库中表现为订单表上的“PersonID”外键。
当某个人没有订单时,该人的 Orders 集合属性将返回 null。我期望它已使用空集合进行初始化,以避免到处都需要空检查代码。
当数据库中还没有该集合的任何项目时,有什么方法可以配置 nHibernate (流畅地)返回一个空集合,而不是 null ?
感谢您的任何帮助。
I have a standard one-to-many relationship between two objects, Person and Order. This is represented in my object model as a collection of orders on the Person. This manifests itself in the database as a "PersonID" foreign key on the Order Table.
When a person has no orders, the Orders collection property on the Person is being returned as null. I was expecting it to have been initialized with an empty collection, to avoid the need for null checking code all over the place.
Is there any way to configure nHibernate (fluently) to return an empty collection, instead of null when there are not yet any items in the database for that collection?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 Person 类的构造函数中新建 Order 集合。
如果一个人现在没有订单,它将返回一个空集合。
You should new up your Order collection in the constructor of the Person class.
If a Person has no orders now, it will return an empty collection.