在实体框架中使用存储过程

本文关键字:存储过程 实体 框架 | 更新日期: 2023-09-27 18:33:39

>我正在尝试将以下 sproc 添加到实体框架中。通过"从模型更新"添加此内容后,模型浏览器会在模型的"函数导入"和"存储过程/函数"中显示此 sproc。使用"函数导入"对话框中的"编辑",我无法"获取列信息",并且无法成功确定集合的返回类型。

sproc 的输出是一个临时表,但我确实定义了要返回的列。这是问题所在吗?我是否缺少设置 EF 的步骤?


 ALTER PROCEDURE [dbo].[addrApproxSP] 
-- Add the parameters for the stored procedure here
@frontage bigint = 0, 
@housedir varchar(1) = 0,
@streetnum bigint = 0, 
@streetdir varchar(1) = 0,
@distance bigint = 0
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Set Variables
DECLARE @lowfront bigint,
        @highfront bigint,
        @lowstreet bigint,
        @highstreet bigint,
        @currname varchar(25),
        @streetcur varchar(25),
        @whereclause varchar(40),
        @fixstreet varchar(25),
        @pos int,
        @piece varchar(500)
--Set variables to proper values in range
    Set @lowfront = @frontage - @distance
    set @highfront = @frontage + @distance
    set @lowstreet = @streetnum - @distance
    set @highstreet = @streetnum + @distance

-- Process for Street Names that are Numeric Values
-- Create Temp Table
CREATE TABLE #StreetNames
(streetname varchar(25))
-- SELECT StreetName and put in Temp table
INSERT #Streetnames(streetname)
select distinct streetname
from ADDR_STREETCOORD
where begincoord between @lowstreet and @highstreet
and STREETDIR = @streetdir
and lowhouse > @lowfront and HIGHHOUSE < @highfront
and HOUSEDIR = @housedir
union
select distinct streetname
from ADDR_STREETCOORD
where lowhouse > @lowstreet and HIGHHOUSE < @highstreet
and HOUSEDIR = @housedir
and begincoord between @lowfront and @highfront
and STREETDIR = @streetdir
-- Check each Streetname and those that are a coordinate (ex: "1000 S") change to "1000"
CREATE TABLE #FixStreets(streetname varchar(25))
DECLARE curStreet CURSOR FOR SELECT streetname FROM #StreetNames
    OPEN curStreet            
    FETCH NEXT FROM curStreet INTO @fixstreet  
    WHILE @@FETCH_STATUS = 0  
    BEGIN 
            --insert code here 
    INSERT #FixStreets(streetname)
        --Call Function to: parse the street name
        --if the first part isnumeric then insert that
        --if not numeric then keep as is
        select [dbo].fnParseCoordinate(@fixstreet)
        FETCH NEXT FROM curStreet INTO @fixstreet
    END 
CLOSE curStreet   
DEALLOCATE curStreet 
--select * from #FixStreets
--create a temp table to store the results of each street name in a single table
-- in order to return the results as a single table
--For Each street name search its frontage range values
--loop through each streetname to get the parcels matching those streets
CREATE TABLE #AllResults(Parcel varchar(14),Prop_locat varchar(50))
DECLARE curName CURSOR FOR SELECT streetname FROM #FixStreets
    OPEN curName            
    FETCH NEXT FROM curName INTO @streetcur  
    WHILE @@FETCH_STATUS = 0  
    BEGIN 
            --insert code here 
    INSERT #AllResults(Parcel, Prop_locat)
        select  parcel_id,prop_locat
        from ADDR_ParcelsWWW
        where StreetName = @streetcur
        and predir = @housedir
        and housefrom between @lowfront and @highfront
        union
        select parcel_id,LOCATOR_ADDRESS
        from ADDR_MASTERADDRESS
        where StreetName = @streetcur
        and predir = @housedir
        and housefrom between @lowfront and @highfront
        FETCH NEXT FROM curName INTO @streetcur
    END 
CLOSE curName   
DEALLOCATE curName 
--Select the results of all the tables
select Parcel, prop_locat from #AllResults
END

在实体框架中使用存储过程

当 EF 轮询您的 SP 时,它会首先执行以下操作:将 FMTONLY 设置为打开

如果您在 SP 中使用临时表,这可能会搞砸事情,看起来您正在这样做。

尝试在 SP 的开头显式设置此项:将 FMTONLY 设置为关闭

这应该允许 EF 检测您的列。