实体框架:存储过程的错误返回类型

时间:2013-11-10 18:18:12

标签: c# entity-framework

我有一个存储过程,你可以在下面找到逻辑,现在当我把这个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

1 个答案:

答案 0 :(得分:1)

我最近遇到过这个问题。 EF无法自动生成SP。我们应该手动添加它。结果,我应用了以下方法。

我的例子SP:

CREATE TABLE #LocalTempTable
(
    c1 INT,
    c2 INT, 
    c3 NVARCHAR(50),
    c4 INT
)

SELECT * FROM #LocalTempTable

添加新的复杂类型(对于临时表返回列):

解决方案资源管理器中的1-开放.edmx文件

2 - 右键单击​​以从打开的窗口中清空空间然后&#34;添加新 - &gt;复杂类型&#34;

3 - 重命名类型并右键单击模型浏览器上的重命名类型然后&#34;添加 - &gt;标量属性 - &gt; COLUMN_TYPE&#34;

我的SP返回4列。 int32int32stringint32。如果返回类型为nullable,则可以从属性窗口更改

5-现在我们可以从数据库导入SP。右键单击“模型浏览器”上的“函数导入”,然后“#34;添加函数导入&#34;

6-编写自定义名称(注意:使用此名称代码隐藏)Chosee存储过程。勾选复杂,选择创建的复杂类型然后确定。

现在测试SP

DBEntities db = new SIRAMATIKEntities();
List<complex_type> result= db.FunctionName(data).ToList();

如果返回的列名称为不同的复杂类型columb,则可以从Mapping Details更改。在模型浏览器中右键单击已创建的功能,然后单击&#34;功能导入映射&#34;。

我希望它有所帮助。如果拼写错误,我很抱歉。我用翻译。