How does NHibernate handle null values for non-nullable fields?
I'm using NHibernate and I have some doubts abour how NHibernate handles null values for non-nullable fields. I would assume that NHibernate would throw an exception at the time of creating the entity, but I have foun开发者_如何学Cd some evidence suggesting it will set the field to the default value of the type.
Which is correct?
From what I have experienced, it would usually assign a default value.
- If you have an entity with an DateTine property and you don't assign anything to it, it would assign a default date, usually the oldest one the database can handle.
- If you have an entity with an Enum property, it could simply assign the first value of the Enum (e.g., Enum { Off = 0, On = 1 }, it would take 0 by default).
- Numeric types would of course get a 0 value.
This is the behaviour I have found in my applications. One could probably somehow make it throw exceptions, but I personally have no need for it.
精彩评论