使用流畅的 NHibernate 进行一对多级联插入
我有两个使用流畅的 NHibernate 映射的类 - User 和 UserRoleAssignment。一个用户有许多用户角色分配。这是用户的相关映射:
HasMany(x => x.Roles)
.Table("UserRoleMap")
.Cascade.SaveUpdate();
对于 UserRoleAssignment:
Map(x => x.User_Id);
如您所见,我仅从 User 引用到 UserRoleAssignment。在 UserRoleAssignment 中,我仅映射外键列 (User_Id)。 问题是,当我保存用户时,出现由外键约束引起的异常,因为 NHibernate 在 User_id 属性的值中插入 0。这只发生在新用户上 - 现有用户(已经有 ID)工作正常。
那么问题是,如何确保NHibernate将User_Id的值设置为插入新User时生成的ID?
多谢!
I have two classes mapped using fluent NHibernate - User and a UserRoleAssignment. An User has many UserRoleAssignments. Here's the relevant map for user:
HasMany(x => x.Roles)
.Table("UserRoleMap")
.Cascade.SaveUpdate();
And for UserRoleAssignment:
Map(x => x.User_Id);
As you can see, I'm only referencing from User to UserRoleAssignment. From UserRoleAssignment, I'm only mapping the foreign key column (User_Id).
The problem is, that when I save the user, I get an exception caused by the foreign key constraint, because NHibernate is inserting 0 in the value for the User_id property. This only happens on new users - existing users (which already has an ID), works fine.
So the question is, how can I make sure that NHibernate sets the value of User_Id to the ID generated when inserting the new User?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这记录在 http://nhibernate.info/doc/nh/ en/index.html#collections-onetomany:
This is documented in http://nhibernate.info/doc/nh/en/index.html#collections-onetomany: