fluent nHibernate map YesNo when column is nullable

青春壹個敷衍的年華 提交于 2019-12-10 22:58:16

问题


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?


回答1:


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/




回答2:


If you want to allow nulls, make your field bool? and it will be null in the database too.



来源:https://stackoverflow.com/questions/9635292/fluent-nhibernate-map-yesno-when-column-is-nullable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!