Type mismatch error

37 views
Skip to first unread message

אלכסנדר פוסמן

unread,
Jan 9, 2022, 11:51:49 AM1/9/22
to nhusers
Hi 

I am trying to upgrade C# code from old nHibernate version to latest one. A lot of people with various degree of knowledge in nHibernate was working on this projects so sometimes it used very deeply involved in code of nHibernate and sometimes it is obvious. The upgrade process goes well - usually changes from <bag> to <set> and I learn a lot about nHibernate but there's a very strange error in some query that look like this:

NHibernate.QueryException: 'Type mismatch in NHibernate.Criterion.SimpleExpression: OwnerBusinessUnit expected type BusinessUnit, actual type BusinessUnit'

I am assure you that there's only 1 and only one class named BusinessUnit (the name is shorten because it is private code)

I thought that I found a solution - I changed something in HBM file (it even does not matter what as I found out) and it start to work Ok but then suddenly it start to throw this exception again (??? how come ?). So I found another solution - I upgraded nHibernate nuget package to the latest and everything was working OK then suddenly it start to throw this exception again.

Could someone help me in this situation ? The error message is very confusing and I don't really know where to start looking for solution.....


Frédéric Delaporte

unread,
Jan 9, 2022, 12:18:32 PM1/9/22
to nhusers
Hi,
The only cases I have seen such troubles is with dynamically compiled Web sites. Sometimes IIS does recompile code it has already compiled, and then the application starts using the same class from different dynamically compiled assemblies. That causes them to be no more considered of being of the same type. This case has nothing to do with NHibernate. (NHibernate is not used in the application with which I have seen that.)
Restarting the application causes the trouble to go away. Ensuring that all folders with dynamically compiled code are compiled soon after startup, by having some process browsing the Web site, highly reduces the occurrences of the trouble. Forcing an application restart if some patch touches dynamically compiled files which changes are not enough to trigger a global restart is also needed. (This can be done by touching a file under the App_Code special folder.)
Another way to avoid having the trouble is to change to compiled at build time classes shared between different dynamically compiled folders being outside of App_Code.

Is your application an "old style" Web site converted to a Web application but still having dynamically compiled code? I means, code which has to be deployed as source code to the IIS server, instead of being compiled to an assembly. Such .cs or .vb files (including aspx.cs and aspx.vb files) have their build action being Content instead of Compile. Maybe even aspx template file can have this trouble, if they use classes dynamically compiled from the App_Code special folder

wil...@softworks.co.za

unread,
Jan 9, 2022, 12:19:15 PM1/9/22
to nhusers
Only a gutfeel. If it works then fails, upgrade - it works then it fails - it must be your code and probably is dynamic and data dependent. Write a test with defined data and try again.

אלכסנדר פוסמן

unread,
Jan 11, 2022, 3:13:16 PM1/11/22
to nhusers
Hi 

Thank you all for your answers ! The solution was very simple in my case. Hard to find but simple to solve. Actually I have duplicate file with code that contains class BusinessUnit. It seems that someone just added DLL with strange name that was exact copy of original DLL. So although in Visual Studio I was looking at one and only one project that contains class BusinessUnit - actually on disk there was 2 files. Because we loading files dynamically (aka plugin style) application loaded all files from folder so there was 2 objects named BusinessUnit in memory. Somehow nHibernate loaded different object each time (?) and this lead to error above when expected type and actual type was the same....

Thank you for your help !!!
Reply all
Reply to author
Forward
0 new messages