tags:

views:

45

answers:

1

I have a model with a Dictionary<string, string> property that is stored in the database using JSON. I am using a custom NHibernate type to map the dictionary to and from a JSON-encoded string, which works fine.

My problem is that when I flush the session, and the only property that has changed is this dictionary, then the entity is not updated. If any other property has also changed, then the entity (including the JSON string) is updated.

In other words, NHibernate correctly maps the dictionary to JSON using my custom type, but this custom property is NOT used to determine if the entity has changed.

Suggestions?

+3  A: 

You need to correctly override Equals in your IUserType implementation so that it returns false when it's compared to its initial value. NHibernate uses Equals to determine if the field has changes that need to be persisted.

The reason it's updated when another property has changed is that, by default, NHIbernate issues updates including all mapped properties.

Jamie Ide
Thanks, my Equals() implementation was the problem... I was returning true if both object references pointed to the same object, and in my application code I was simply modifying the dictionary rather than creating a new one. I think the easiest fix is to change my app to create a new dictionary object when it updates the model.
Seth Petry-Johnson