开发者

nchar trailing spaces causing record to appear dirty in nHibernate

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 traili开发者_如何学编程ng 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜