asp.net - 如何将SQL用户自定义函数添加到 Entity Framework 中?

标签 asp.net sql-server linq entity-framework-4

我可以像在 .dbml 中那样向 .edmx 文件添加 SQL 函数吗?如果可以的话,我该怎么做?如果不能,有什么解决方法吗?

我尝试在 Google 上搜索,但找不到任何有关如何执行此操作的具体答案。

根据给定的答案,我创建了一个存储过程并尝试添加“导入函数”,但它显示“存储过程不返回列”。我哪里做错了? 功能:

ALTER FUNCTION [dbo].[fn_locationSearch](@keyword varchar(10))
RETURNS TABLE
AS
RETURN
(
SELECT CustomerBranch.ID,CustomerBranch.BranchName,CustomerBranch.Longitude,CustomerBranch.Latitue,CustomerBranch.Telephone,CustomerBranch.CategoryID,CustomerBranch.Description

FROM FREETEXTTABLE (CustomerOffer,*,@keyword) abc INNER JOIN OffersInBranch
ON abc.[key]=OffersInBranch.OfferID INNER JOIN CustomerBranch ON     OffersInBranch.BranchID=CustomerBranch.ID
UNION
SELECT    CustomerBranch.ID,CustomerBranch.BranchName,CustomerBranch.Longitude,CustomerBranch.Latitude,CustomerBranch.Telephone,CustomerBranch.CategoryID,CustomerBranch.Description
FROM CustomerBranch WHERE FREETEXT(*,@keyword)
)

存储过程:

ALTER PROCEDURE USP_locationSearch
(@keyword varchar(10))
AS
BEGIN
SELECT * from dbo.fn_locationSearch(@keyword)
END

最佳答案

Entity Framework 中没有对 SQL 用户定义函数的内置支持,最好的方法是创建一个存储过程来包装函数调用并返回其输出,然后将该过程添加到您的 EF 模型中。

关于asp.net - 如何将SQL用户自定义函数添加到 Entity Framework 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8051299/

相关文章:

asp.net - 将 ASP.Net MVC 的单个页面添加到现有的 Web 窗体应用程序?

sql-server - 检查列的所有值

sql-server - 获取SQL Server图数据库中的所有好友

C# 获取不同的最大值

sql - 使用 LINQ 动态改变 'where' 语句中的条件数

c# - 带有 angularjs 的 $http 请求正在返回我的 html 文档

c# - ASP.Net Web 部件 : How to hide a webpart?

c# - 从 CodeBehind 页面显示 Javascript 不起作用!

c# - Linq 类别和子类别

sql-server - SQL Server 如何确定序列中的下一个值?