How do I serialize/deserialize a NHibernate entity that has references to other objects?
Posted
by Daniel T.
on Stack Overflow
See other posts from Stack Overflow
or by Daniel T.
Published on 2010-05-07T04:21:18Z
Indexed on
2010/05/07
4:28 UTC
Read the original article
Hit count: 214
I have two NHibernate-managed entities that have a bi-directional one-to-many relationship:
public class Storage
{
public virtual string Name { get; set; }
public virtual IList<Box> Boxes { get; set; }
}
public class Box
{
public virtual string Box { get; set; }
[DoNotSerialize] public virtual Storage ParentStorage { get; set; }
}
A Storage
can contain many Boxes
, and a Box
always belongs in a Storage
. I want to edit a Box's
name, so I send it to the client using JSON. Note that I don't serialize ParentStorage
because I'm not changing which storage it's in.
The client edits the name and sends the Box
back as JSON. The server deserializes it back into a Box
entity.
Problem is, the ParentStorage
property is null. When I try to save the Box
to the database, it updates the name, but also removes the relationship to the Storage
.
How do I properly serialize and deserialize an entity like a Box
, while keeping the JSON data size to a minimum?
© Stack Overflow or respective owner