recently, we had to clean the database from all the testing data, when an error similar to the following appeared:
The value “” is not of type “System.Nullable`1[System.DateTime]” and cannot be used in this generic collection
The code I executed was:
IQuery query = DbManager.MySession.CreateQuery(“select max(dateObject.EndDate) from DateDomain dateObject”);
IList<DateTime?> list = query.List<DateTime?>();
When I debugged NHibernate code, I reached to the following AddAll() method in the ArrayHelper class:
152 // NH-specific
153 public static void AddAll(IList to, IList from)
154 {
155 foreach (object obj in from)
156 {
157 to.Add(obj);
158 }
159 }
You can find the explanation of the error here.
Which brings us to the interesting question: why the implementation of IList Add method doesn’t consider the “nullability” of the T object? and why the parameter is of type IList ?!
Am I missing something? should I report it as a bug?
At the end of the day, I’ve fallen back on wrtinig a custom data access layer with LINQ to SQL in order to finish the project. I could do the same thing with NHibernate, but I’m not going to introduce a new non-Microsoft dependency to a project someone else will most likely end up maintaining if it doesn’t gain me anything, that’s like asking someone to learn calculus before I let them balance my checkbook if learning calculus meant I didn’t have to balance the checkbook by hand, it might be worth it I’m not content with the end result because the business objects aren’t POCO (Plain Old CLR Objects) now (I could make POCO objects, but taking the persistence out means I loose lazy loading). Also, I had to leave a few EntitySet collection properties exposed, because LINQ goes into a tail spin during my unit-tests if I try to actually claim IEnumerable on the Factor objects I had never seen the Operation could destabilize the runtime error before!