2012-01-14 136 views
1

(使用MVC4 VB EF4 MSSQL剃刀)调用存储过程(MVC4)

我在MS SQL 2008数据库中创建一个存储过程。然后我将SP添加到实体框架模型中(打开.edmx文件后没有看到它,当我打开模型浏览器时看到SP)。接下来我做了一个“添加功能导入...”。我做了[获取列信息]和“创建新的复杂类型”。

所以现在我想使用该SP。而使用ExecuteStoreQuery似乎是一种方法。

迄今为止最好的尝试是这样的:

Function Index() As ViewResult 
Dim context As New MyEntities 
Dim Result 
' Call the SP with parameter "A" 
Result = context.ExecuteStoreQuery(Of MySP_Result)("MySP @p0", "A").ToList 
Return View(Result) 
End Function 

其中“MySP_result”是由SP(我看到它的EF模型浏览器)返回的复杂类型的名称。按F5之后,我得到:

传递到字典的模型产品类型 System.Collections.Generic.List, ,但本词典需要 类型的模型项目System.Collections.Generic.IEnumerable

那么我需要改变什么?

回答

0

标准genereated列表视图的开头为:

@ModelType Mvc4App2.[classname] 

我花了一段时间来正确理解错误消息。您需要更改该行到:

@ModelType IEnumerable(Of Mvc4App2.[name of the complex type]) 

我也改变了控制器代码一点:

Dim context As New [Name of the EF model] 
Dim Result As List(Of [name of the complex type]) 
Result = context.ExecuteStoreQuery(Of [name of complex type])_ 
       ("[name of the stored procedure @p0", "[parameter 0]").ToList 
Return View(Result)  
End Function