Saturday, March 8, 2014

Combining F# and Simple.Web to Break SqlEntityConnection

Before we begin, I want to make it absolutely clear that I believe the problem I ran into this week can be attributed, 100%, to PEBKAC.

So, I am creating a new application that is supposed to "enhance" (replace) an internal application and I want to show off how productive F# has been lately. One of the coolest things I've seen with F# are type providers. To connect to the legacy data in this application, I used the SqlEntityConnection and everything was running along smoothly. (I was basically using the type provider as a read-only view of the legacy data, with a few writes here and there.) I ran into a really weird issue that caused me to lose a little bit of time and just want to document it here so I could maybe save future me (or future you) some headache.

I've pushed up a Git repo with a solution that can be run to demonstrate this issue. So, here we go.

The Symptoms

So, I had about 25 Simple.Web handlers firing on all cylinders and, suddenly, one handler would not work. It continued to return a 500 and I couldn't get a breakpoint to catch. Well, I couldn't even get a breakpoint to set. Every one I'd put in gave me an issue saying that symbols were not loaded for the assembly. No matter how much I did "Rebuild All" it would not change. I finally noticed a weird error that looks like this:

A first chance exception of type 'System.NotSupportedException' occurred in FSharp.Core.dll
System.NotSupportedException: LINQ to Entities does not recognize the method 'BlowingUpSqlEntityConnection.Web.Index CheckThis[Index](BlowingUpSqlEntityConnection.Web.Index)' method, and this method cannot be translated into a store expression.
   at Microsoft.FSharp.Linq.QueryModule.CallGenericStaticMethod@337.Invoke(Tuple`2 tupledArg)
   at Microsoft.FSharp.Linq.QueryModule.clo@1741-1.Microsoft-FSharp-Linq-ForwardDeclarations-IQueryMethods-Execute[a,b](FSharpExpr`1 )
   at BlowingUpSqlEntityConnection.Web.Index.getEntity() in C:\Users\James\documents\visual studio 2013\Projects\BlowingUpSqlEntityConnection.Web\BlowingUpSqlEntityConnection.Web\Home.fs:line 15
   at BlowingUpSqlEntityConnection.Web.Index.Simple-Web-IGet-Get() in C:\Users\James\documents\visual studio 2013\Projects\BlowingUpSqlEntityConnection.Web\BlowingUpSqlEntityConnection.Web\Home.fs:line 29
A first chance exception of type 'System.NullReferenceException' occurred in BlowingUpSqlEntityConnection.Web.dll

It's like Entity Framework is trying to run a stored procedure that matches my handler. Weird. Here's what the handler looks like (taken from the available repo).

With a simple change everything worked just fine. By pulling out the Id and making it an parameter of the getEntity function everything went swimmingly.

And now the application is happy as a clam.

No comments:

Post a Comment