Prevent NHibernate from trying to delete association collection
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 cl开发者_JAVA百科ient 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
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
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.
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?
精彩评论