2

I'm trying to map a stored procedure at my database to a function on EF to build queries within my code with LINQ.

My stored procedure returns/selects (tried BOTH, no difference) an integer value. I can map into a function, and see it in code (with correct input/output types), but whenever I try to execute the query, I get this exception:

LINQ to Entities does not recognize the method 'System.Data.Objects.ObjectResult`1[System.Nullable`1[System.Int32]] HammingDistance(System.Nullable`1[System.Int64], System.Nullable`1[System.Int64])' method, and this method cannot be translated into a store expression.

The LINQ that causes the error is this:

db.HammingDistance(targetPhoto.Analysis.Hash, p.Analysis.Hash).FirstOrDefault().Value < 10

which is in a where clause. Without this part, the query runs with no problems.

Yes, my procedure called HammingDistance takes in two longs and returns an int. I tried to create it as a sql server function instead of a stored procedure too, but then I cannot import it as a function, which is what I want. I have complex queries involving the procedure (and tested on management studio, it DOES work and RETURN the value correctly), there are no nulls neither on both the DB or the code, it's not an issue. I've double checked my procedure IS there and working AFTER the error too, with no changes to the SQL code. But LINQ doesn't recognize this. And finally, YES, I need this to run on SQL Server, I can't handle pulling all the fields from the DB and run on ASP.NET. I know it can be done, and I'm pretty sure this is a configuration issue, as both my DB and website and LINQ queries runs perfectly without calling the HammingDistance, but I just can't tie them at that point.

2
  • You can't execute a stored procedure as part of a where clause in SQL. I don't see how Entity Framework is going to work around that, if the queries are going to map to SQL. Jul 30, 2011 at 17:03
  • It should be technically possible. All the query will be translated to an SQL query to run against the database, and that procedure IS in the database, exactly at the place that I want it to run on. There should be a mapping somewhere, even if my implementation is wrong, it should be done. Jul 30, 2011 at 17:06

2 Answers 2

2

You cannot call stored procedure from linq-to-entities query. It is the same as when using SQL directly - you cannot have stored procedure call as part of SQL SELECT. You must create SQL function instead and import it to your EF model.

4
  • I'll be looking into that. But it should be technically possible. All the query will be translated to an SQL query to run against the database, and that procedure IS in the database, exactly at the place that I want it to run on. There should be a mapping somewhere, even if my implementation is wrong, it should be done. Jul 30, 2011 at 17:06
  • No it should not. Just open management studio and run SELECT * FROM YourTable Where YourStoredProcedre(x, y) - you will get an error. No mapping for using stored procedures in linq-to-entities exist and using stored procedure for the problem you describe (HammingDistance) is wrong. You should read about difference between stored procedure and SQL function. Jul 30, 2011 at 17:11
  • Well, I looked at the link, implemented a function with the EdmFunction attribute, and whenever it gets to be executed, it throws a NotSupportedException saying "Unable to create a constant value of type [name of my database context class]. Only primitive types ('such as Int32, String, and Guid') are supported in this context." Tried googling the error but not found anything useful for my scenario so far. Jul 30, 2011 at 18:54
  • Ok, very weird, the function threw that error when I created an extension method, and I created a regular method and it worked. Quite weird, have no idea why it happens, probably something to do with the attribute. Jul 30, 2011 at 19:33
1

Turn HammingDistance into a scalar function, then create either a stored procedure to do the select or create a view.

You can import either of these into your model and get your entities back that way.

Alternatively, create a view that adds a new computed column for the Hamming Distance, then you can use the column in your where clause - bit it will add an extra property to your entity.

3
  • I did the first option and now I'm trying to map the function using the link from the first answer. For the view option, yes, but I haven't ever worked with views, so just getting the function to run is more suitable for my problem. Jul 30, 2011 at 19:32
  • 1
    Fair enough. Just so you know - this is the method that I use and it works just fine. Jul 30, 2011 at 19:42
  • I'll be looking into it too, as I need to, at least learn to work with views anyway. But I think I first need to analyze what EF exactly queries as what to include in the view that I return. Jul 30, 2011 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.