从SQL Server功能导入实体框架运行时异常实体、框架、异常、功能

2023-09-06 05:53:17 作者:掌櫃,我把愛典當給你

我想在我的数据库映射存储过程在EF一个函数,在我的code使用LINQ构建查询。

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

我的存储过程返回/选择(都试过,没有区别)的整数值。我可以映射到一个函数,并看到它在code(使用正确的输入/输出类型),但每当我试图执行查询,我得到这个异​​常:

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.

这会导致错误的LINQ是这样的:

The LINQ that causes the error is this:

db.HammingDistance(targetPhoto.Analysis.Hash,p.Analysis.Hash).FirstOrDefault()值小于。 10

这是一个其中,条款。如果没有这一部分,在查询运行没有问题。

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

是的,我的程序叫HammingDistance接受两个多头并返回一个int。 我试图创建一个SQL Server的功能,而不是存储过程也是如此,但我不能为一个函数,这是我想要的导入。我有涉及的程序复杂查询(和管理工作室的测试,它的工作,正确返回值),有没有空既不在两个数据库或code,这不是一个问题。我双重检查我的方法是存在的,工作的错误后也没有改变SQL code。但LINQ不承认这一点。最后,是的,我需要这个到SQL Server上运行,我不能处理所有的拉动从数据库的字段和运行ASP.NET。我知道这是可以做到的,我pretty的肯定,这是一个配置的问题,因为我的两个数据库和网站和LINQ查询运行,完全不调用HammingDistance,但我不能在这一点上配合他们。

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.

推荐答案

您不能从LINQ到实体查询调用存储过程。这是一样的,直接使用SQL的时候 - 你不能存储过程调用的SQL SELECT的一部分。您必须创建,而不是SQL函数和导入到您的EF模型。

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.

 
精彩推荐