防止 NHibernate 尝试删除关联集合
我正在将 NHibernate 与 RIA 服务一起使用,以下关联给我带来了问题。
User hasMany UserRoleGrants via the property user.Roles
当我尝试更新我的用户时,silverlight 客户端将用户实体传递到服务层,但没有填充角色属性(似乎合理,因为它可能是一个大集合,所以为什么通过网络来回发送它)。这导致 NHibernate 尝试删除该用户的 UserRoleGrants。
感觉就像我想做 session.Load 的等效操作,但只是针对 user.Roles 属性而不是整个对象。
我怎样才能实现这个目标?
编辑:这是我的 Fluent 映射
public UserMap()
{
Id(x => x.Id).GeneratedBy.HiLo("10000");
Map(x => x.UserName).Unique().Not.Nullable().Length(255).Default(string.Empty);
HasMany(x => x.RoleGrants).KeyColumn("User_id").Cascade.None().Access.CamelCaseField().LazyLoad();
}
谢谢
I'm using NHibernate with RIA services the following association is causing me problems.
User hasMany UserRoleGrants via the property user.Roles
When I try to update my User the silverlight client passes the User entity to the service layer but without the Roles property populated (seems reasonable as it might be a large collection so why send it back and forth over the network). This is causing NHibernate to try to delete the UserRoleGrants for this user.
It feels like I want to do an equivalent of session.Load but just for the user.Roles property rather than the whole object.
How can I achieve this?
Edit: Here's my Fluent mapping
public UserMap()
{
Id(x => x.Id).GeneratedBy.HiLo("10000");
Map(x => x.UserName).Unique().Not.Nullable().Length(255).Default(string.Empty);
HasMany(x => x.RoleGrants).KeyColumn("User_id").Cascade.None().Access.CamelCaseField().LazyLoad();
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您不应该让实体跨越服务边界。请改用一些 DTO 并将其发送到服务。这个问题可能会有所帮助:How to use NHibernate and DTOs with RIA Services
probably you should not let the entity cross the service boundary. Use some DTO instead and send it to the service. This question could help: How to use NHibernate and DTOs with RIA Services
通常我们在服务层之间使用 DTO。
如果您从数据库中获取用户,更新字段并保存,角色集合将不会受到干扰。它是延迟加载且未更改,因此不会被删除。
Generally we use DTOs to/from the service layer.
If you get the User from your database, update the fields and save, the roles collection will not be disturbed. It is lazy loaded and unchanged thus it will not be deleted.
如果您想初始化集合,您可能需要使用 NHibernateUtil.Initialize() (请参阅 这个,第 16.1.4 节。)。
但是,如果您不在任何地方使用该集合,则加载它并不是一个好主意。
您是否考虑过使关系非级联?
if you want to initialize your collection, you might want to use NHibernateUtil.Initialize() (see this, section 16.1.4.).
however- if you're not using that collection anywhere, it's not a very good idea to load it.
have you considered making the relation non-cascading?