流畅的nHibernate映射是当列可为空时是否 [英] fluent nHibernate map YesNo when column is nullable
问题描述
我使用流畅的nHibernate将数据库标志列Y/N映射到 bool
属性:
Map(x => x.Enabled).Column(ENABLED_FLAG)。CustomType(YesNo);
问题是,如何指定如何映射null?将null映射为true,false,exception?
更新
默认设置似乎将NULL假。我喜欢这一点,但仍然想知道如何可以重写这是真实的?
如果你想改变的功能null的情况下,你将不得不创建自己的自定义类型 - 本质上来自IUserType。
我已经做了类似的日期(其中01-01-0001不能保存到mssql的0日期)和Guids我们想要插入null创建你自己的用户类型可以覆盖NullSafeSet和NullSafeGet方法 - 这就是你想要做的事情(改变对null的处理)读或写)
你甚至可以继承原来的YesNo Type
I'm using fluent nHibernate to map a a database flag column "Y"/"N" to bool
property:
Map(x => x.Enabled).Column("ENABLED_FLAG").CustomType("YesNo");
The question is, how would one specify how to map a null? Will the null be mapped to true, false, exception?
Update
The default settings seems to map NULL to false. I like that, but still wondering how I could override that to be true?
If you want to change the functionality of the null case you would have to create your own custom type - essentially derive from IUserType.
I have done something similar to this with dates (where a 0 date of 01-01-0001 cant save to mssql) and with Guids where we want to insert null instead of Guid.Empty)
Creating your own user type gives the ability to override the NullSafeSet and NullSafeGet methods - which is what you want to do (change the handling of Nulls at read or write) You might even be able to inherit from the original YesNo Type
A good example http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/
这篇关于流畅的nHibernate映射是当列可为空时是否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!