2012-01-14 127 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