3

The nchar starting spaces causing a record to appear in nHibernate

 2 years ago
source link: https://www.codesd.com/item/the-nchar-starting-spaces-causing-a-record-to-appear-in-nhibernate.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The nchar starting spaces causing a record to appear in nHibernate

advertisements

I am finding that whenever I load an object from my database it is immediately appearing as being Dirty.

I found some code that will let me see if the object is dirty here: http://nhforge.org/wikis/howtonh/finding-dirty-properties-in-nhibernate.aspx

var x = session.Get<MyRecord>(123);
var dirtyEntity = session.IsDirtyEntity(x);

dirtyEntity is always evaluating to true to entities of this class.

Looking into it a bit more I think I've found the root of the problem. I have a property which is mapped onto an nchar(15) column in SQL Server. The value in the DB has trailing spaces, but the value appearing on the object has been trimmed. So... the following is returning true.

var x = session.Get<MyRecord>(123);
var dirtyProperty = session.IsDirtyProperty(x, "Status");

Does anyone know how I can get nHibnernate to say that "Status OK" and "Status OK      " are equivelent, and that the entity is not dirty?


This may be solvable by using an IUserType for the property. There is a TrimString example in UNHAddins; you can read about its usage here.

Tags nhibernate

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK