调用存储过程(MVC4) [英] Calling a Stored Procedure (MVC4)

查看:284
本文介绍了调用存储过程(MVC4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(使用MVC4 VB EF4 MSSQL剃刀)

(Using MVC4 VB EF4 MSSQL Razor)

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

I created a Stored Procedure in the MS SQL 2008 database. Then I've added that SP into the Entity Framework model (you do not see it after opening the .edmx file, i see the SP when i open the model browser). Next i did an "Add function import..." . I did [Get Column Information] and "Create new complex type".

所以,现在我想使用该SP。并使用ExecuteStoreQuery似乎要走的路。

So now I'd like to use that SP. And using ExecuteStoreQuery seems the way to go.

最好的尝试,到目前为止是这样的:

The best attempt so far is this:

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模型浏览器)返回的复杂类型的名称。 pressing F5后我得到:

Where "MySP_result" is the name of the Complex Type that is returned by the SP (i see it in the EF model browser). After pressing F5 i get:

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

The model item passed into the dictionary is of type System.Collections.Generic.List, but this dictionary requires a model item of type System.Collections.Generic.IEnumerable

所以,我有什么改变?

推荐答案

一个标准genereated列表视图开头:

A standard genereated list view starts with:

@ModelType Mvc4App2.[classname]

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

It took me a while to understand the error message correctly. you need to change that line into:

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

我也改变控制器code一点:

I also changed the controller code a little:

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       

这篇关于调用存储过程(MVC4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆