blog.mha.dk
The on-line blog of Michael Holm Andersen

InvalidCastException on linq DB submit with non-integer key – aka LinqToSQL bug

Friday, 27 November 2009 09:02 by mha

Wasted a whole day on a (confirmed) bug in LinqToSQL. The problem is this:

“When database contains two tables, both with automatic integer primary keys, and a relationship between a unique char field in one and a non-unique char field in the other, inserting new rows into the second table fails on submit with InvalidCastException"

The error returned be LinqToSQL is: specified cast is not valid

I have a table which contains a char column which is ForeignKey to a unique char column in another table. In my case the column is “CurrencyISO” (e.g. the value DKK) in one table which has a FK relation to a unique ISO4217_ISO column in my Currency table.

Running on my development machine (Windows 7, x64) the code executes without problem – however running on my development server (Windows Server 2008 Web edition, x64) LinqToSQL at random (more or less!) throws the exception: specified cast is not valid

After having spend hours on the bug (I thought it was related to different OS and/or differences in MS SQL Server or something other “weird”), I found this article on Microsoft Connect which confirms the bug and the message “I can confirm that this bug has been fixed for .NET 4.0, which is why it's still failing with .NET 3.5 SP”

However a hotfix is available (and it will fix this bug!) – You can download yourself from Connect or if you’re running an x64 OS (Vista and/or Windows Server 2008), you can simple download the below .ZIP file which contains the hotfix (I’ve installed this on my Windows Server 2008 Web edition (x64), rebooted (an IIS reset should be enough!) and finally the bug was gone!!

Grab the hotfix (NDP35SP1-KB963657-x64.exe) for x64 platforms here:

Categories:   LINQ | SQL/Database
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed
Comments are closed