I was working with LINQ to SQL today to bind some data to a drop down list in ASP.NET MVC, when I encountered this strange exception I’ve never seen before:
System.NotSupportedException: The mapping of interface member IDbTable.StatusID is not supported.
I figured I knew what triggered the problem, and that being due to the new interface I have all my LINQ to SQL entities implement from as to perform some generic query logic in my repository, the IDbTable.
But what I still didn’t know, is why I was receiving this error.
After some careful research, it appears that in the code construct where I build the query, I was performing a conditional test with equal signs (==) instead of the Equals() method. Very strange, but as Philipp quoted, even though it results in the same SQL query executing, one throws an NotSupportedException and one does not!
So all I had to do was to change:
return GetAll().Where(x => x.StatusID == (int)status);
To this:
return GetAll().Where(x => x.StatusID.Equals((int)status));
and I was all set.
This is going to make me closely think twice about EVER using the double equals in LINQ to SQL again.
5 Comments
I had no idea this could make a difference – thanks a lot! Saved me from a bit of pain.
Thank you for this, you definitely post this solution on
http://connect.microsoft.com/VisualStudio/feedback/details/344903/linq-to-sql-mapping-interface-member-not-supported-exception
as a workaround, so other developers can find the solution more quickly.
Thank YOU!
Thanks for posting that.
I just wonder if that .Object method involves boxing/unboxing at all?
Saved my ass! thanks!
I was just about to give up and dump my Generic class when I found this post. Thank you!
Post a Comment