Calling stored procedure in EF with Complex Data Type

时间:2022-11-13 17:03:45

I am new to working with Entity Framework in a sample ASP.NET/MVC 3.0 VS2010 project. I have imported two stored procedures as Function Imports and, in doing so, created a complex data type for each.

我是一个ASP.NET / MVV 3.0 VS2010项目中的Entity Framework新手。我已导入两个存储过程作为函数导入,并在此过程中为每个创建了一个复杂的数据类型。

I am trying to run the following c# code:

我正在尝试运行以下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;
}

And I'm getting the error:

我收到了错误:

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

What am I doing wrong? How do I properly store the results of the stored proc in a complex data type?

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

2 个解决方案

#1


2  

You can use DbSet.SqlQuery method to get results from a stored procedure.

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

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

Assuming you have a collection like this defined your DbContext class.

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

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

More information is available here.

更多信息请点击这里。

#2


1  

Try and change this:

尝试并改变这个:

result = context.StoredProc(param1, param2, param3);

to this:

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

#1


2  

You can use DbSet.SqlQuery method to get results from a stored procedure.

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

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

Assuming you have a collection like this defined your DbContext class.

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

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

More information is available here.

更多信息请点击这里。

#2


1  

Try and change this:

尝试并改变这个:

result = context.StoredProc(param1, param2, param3);

to this:

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