[英]Entity Framework : wrong return type of stored procedure
I have a stored procedure you can find the logic below, and now when I take this sp to EF {version 5.0} this sp would take return type as int. 我有一个存储过程,可以在下面找到逻辑,现在当我将这个sp转到EF {version 5.0}时,这个sp会将返回类型作为int。
Create Proc SP_GetResultBasedOnFilterForCoverage
(
@AgeID int,
@IncomeID int,
@GenderID int,
@EducationID int,
@ProductID int
)
as
set nocount on
Declare @query varchar(max), @index int, @totalrow int, @coverageTotal bigint
IF OBJECT_ID('tempdb..#SecondDataSet') IS NOT NULL
DROP TABLE #SecondDataSet
Create table #SecondDataSet
(
CoverageID int,
Coverage varchar(100),
Percentage float
)
Declare @CoverageTable Table
(
id int identity(1,1),
CoverageID int,
Coverage varchar(100),
CoverageCount int
)
insert into @CoverageTable
(CoverageID,Coverage,CoverageCount)
Select TCPC.CoverageID,Coverage.Coverage, COUNT(CustomerID) from TBL_Customer_Product_Choice as TCPC
inner join
Tbl_Coverage as coverage
on
coverage.CoverageID = TCPC.CoverageID
group by
TCPC.CoverageID,coverage.Coverage
set @index = 1;
Select @totalrow = COUNT(ID) from @CoverageTable
While(@index <= @totalrow)
begin
Declare @CurrentCoverageID int
Select @coverageTotal = CoverageCount, @CurrentCoverageID = CoverageID from @CoverageTable where id = @index
set @query = '
Insert into #SecondDataSet
(Percentage,CoverageID,Coverage)
Select Round((Cast(COUNT(TCPC.CustomerID) as float)/cast('+cast(@coverageTotal as varchar)+' as bigint)) * 100,1) as Percentage ,
TCPC.CoverageID ,TC.Coverage
from Tbl_customer_Product_Choice as TCPC
inner join
Tbl_Coverage as TC
on
TCPC.CoverageID = TC.CoverageID
Where productID = cast('+cast(@ProductID as varchar)+'as int) and TCPC.CoverageID = cast('+ cast(@CurrentCoverageID as varchar)+' as int)'
if(@AgeID <> 0)
begin
set @query += ' and AgeID = ' + CAST(@AgeID as varchar);
End
if( @IncomeID != 0)
begin
set @query += ' and IncomeID = ' + CAST(@IncomeID as varchar);
End
if(@EducationID != 0)
begin
set @query += ' and EducationID = ' + CAST(@EducationID as varchar);
End
if(@GenderID > -1)
begin
set @query += ' and GenderID = ' + CAST(@GenderID as varchar);
End
set @query += ' group by TCPC.CoverageID,TC.Coverage
order by COUNT(TCPC.CustomerID) desc
'
Exec(@query)
set @index = @index + 1;
end
Select * from #SecondDataSet
I recently came across this problem. 我最近遇到了这个问题。 EF can not auto generate SP.
EF无法自动生成SP。 We should add it manually.
我们应该手动添加它。 As a result, I applied the following methods.
结果,我应用了以下方法。
Its my example SP: 这是我的示例SP:
CREATE TABLE #LocalTempTable
(
c1 INT,
c2 INT,
c3 NVARCHAR(50),
c4 INT
)
SELECT * FROM #LocalTempTable
Add a new complex type (for temp table returns column): 添加一个新的复杂类型(用于临时表的返回列):
1-Open .edmx
file from Solution Explorer 1-从解决方案资源管理器中打开
.edmx
文件
2-Right click to empty space from opened windows then "Add New -> Complex Type" 2-右键单击以从打开的窗口中清空空间,然后单击“添加新的->复杂类型”
3-Rename type and Right click renamed type on Model Browser then "Add -> Scalar Property -> COLUMN_TYPE" 3-重命名类型,然后在模型浏览器上右键单击重命名的类型,然后单击“添加->标量属性-> COLUMN_TYPE”
My SP return 4 column. 我的SP返回4列。
int32
, int32
, string
, int32
. int32
, int32
, string
, int32
。 If return type is nullable
you can change from properties window 如果返回类型为
nullable
为nullable
,则可以从属性窗口更改
5- Now we can import SP from Database. 5-现在我们可以从数据库导入SP。 Right click Function Imports on Model Browser then "Add Function Import"
右键单击“模型浏览器”上的“功能导入”,然后单击“添加功能导入”
6- Write a custom name (NOTE: use this name codebehind) Chosee Stored Procedure. 6-写一个自定义名称(注意:在此代码后面使用此名称)选择存储过程。 Tick Complex, select created complex type then ok.
勾选“复杂”,选择创建的复杂类型,然后单击“确定”。
Now test SP 现在测试SP
DBEntities db = new SIRAMATIKEntities();
List<complex_type> result= db.FunctionName(data).ToList();
If returned column names different complex type columb, you can change from Mapping Details. 如果返回的列名使用不同的复杂类型列,则可以从“映射详细信息”中进行更改。 In Model Browser Right click created function then click "Function Import Mapping".
在模型浏览器中,右键单击创建的函数,然后单击“函数导入映射”。
I hope it helped. 希望对您有所帮助。 I'm sorry if spelling mistakes.
如果拼写错误,我感到抱歉。 I use translate.
我用翻译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.