c# - 使用复杂数据类型在 EF 中调用存储过程

标签 c# sql-server asp.net-mvc entity-framework complex-data-types

我刚开始在示例 ASP.NET/MVC 3.0 VS2010 项目中使用 Entity Framework。我已将两个存储过程作为 Function Imports 导入,并在这样做时为每个存储过程创建了一个复杂的数据类型。

我正在尝试运行以下 C# 代码:

public ComplexDataType RunStoredProc()
{
    var context = new DbEntities();
    int param1 = 370;
    int param2 = -1;
    string param3 = "Current";

    ComplexDataType result = new ComplexDataType;
    result = context.StoredProc(param1, param2, param3);
    return result;
}

我收到错误:

Cannot implicitly convert data type 
    System.Data.Objects.ObjectResult(ComplexDataType) to 'ComplexDataType'

我做错了什么?如何以复杂数据类型正确存储存储过程的结果?

最佳答案

您可以使用 DbSet.SqlQuery 方法从存储过程中获取结果。

int param1=12;
int param2=53;
var results=context.ComplexDataTypes.SqlQuery.
                       ("dbo.YourSPNameHere @p0 @p1", param1,param2).Single();

假设您有一个这样的集合定义了您的 DbContext 类。

public DbSet<ComplexDataType> ComplexDataTypes { set;get;}

更多信息可用here .

关于c# - 使用复杂数据类型在 EF 中调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212338/

相关文章:

c# - 自托管 Web Api C# 与 Windows 窗体错误

c# - 什么是最有效(免费提供)的 C# 代码覆盖工具?

sql - 什么是 SQL Server 对 MySQL 的 unicode_ci 排序规则的模拟?

asp.net-mvc - 我的 View 模型应该以我的域实体为洋葱架构的核心吗?

asp.net-mvc - Html.ActionLink() : new { route value} is acting strangely. 我不知道length = 17 参数是从哪里来的

javascript - 从 ViewBag 加载文件?

c# - Selenium WebDriver.ChromeDriver Nuget 包已安装,但不适用于 MSTest

sql-server - update-database -force 命令未更新 Entity Framework 代码中的基础 -first

sql-server - 表位于 sys.objects 中但不在表列表中

javascript - 如何使用输入数组序列化表单