InvalidCastException when deleting entities containing uninitialized lazy components


When trying to delete an entity having an uninitialized lazy component, InvalidCastException is thrown: Unable to cast object of type 'NHibernate.Intercept.UnfetchedLazyProperty' to type xxx.

I believe the problem lies at NHibernate.Engine.ForeignKeys.Nullifier, at NullifyTransientReferences. This method will try to walk through the component's property values regardless they have been initialized or not, and does not initialize the properties itself, nor does it throw any meaningful exception.
It would be nice if the uninitialized properties were initialized, or better the lazy properties were initialized only when there are any properties under the component's hierarchy which need to be nullified.
As we don't use associations and collections in out components, our workaround was to skip "nullification" of uninitialized components at all.

I have tested this using the following setups, and behavior was all the same: .NET 4.0 + Oracle.DataAccess, .NET 4.0 + System.Data.SQLite, .NET 2.0 + System.Data.SQLite

The attached test cases are to be imported into the standard NHibernate.Test project.




July 10, 2014, 12:33 AM

Hello! About your workaround, what do you mean with "nullification" of uninitialized components? Can you provide us an example?

Gabor Szecsi
September 9, 2014, 1:17 PM

I have attached the workaround we used. The xml documentation comments on the classes are in Hungarian, which basically means the same as the bug report in this issue.
As a long time has passed I don't remember this issue exactly. I will try to dig myself into this later, when a little spare time is available for me.

Alex Zaytsev
January 20, 2015, 8:20 AM

Merged to 4.0.x at 569b49c4ead3f026f0a161c1b9b6833fb92e7216

Alex Zaytsev
May 1, 2017, 3:37 AM

Closing issues resolved in 4.0.3


Alex Zaytsev


Gabor Szecsi


Fix versions

Affects versions