tags:

views:

42

answers:

2

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?

+1  A: 

Why don't you use varchar as the data type in database for the said column. This will solve your issue as well as prevent the wastage of space going on in the database.

Faisal Feroz
What if he's using a legacy database he can't modify?
Rafael Belliard
Rafael hit the nail on the head. I'm writing an interface that synchronises two separate systems and I don't have control of the db schema in either.
Chris Kemp
+1  A: 

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.

DanP