开发者

NHibernate change tracking does not pick up changes to a custom type (IUserType)

开发者 https://www.devze.com 2022-12-30 20:06 出处:网络
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,

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, N开发者_如何学CHibernate 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消