EF4 ExecuteStoreQuery获取嵌套列表

时间:2013-03-05 13:44:46

标签: c# entity-framework-4

我有一个包含3个select语句的存储过程:

select col1, col2 from table1
select col1, col2 from table2
select col1, col2 from table3

我为每个表创建了3个模型:

public class table1
{
    public string col1 { get; set; }
    public string col2 { get; set; }
}
public class table2
{
    public int col1 { get; set; }
    public int col2 { get; set; }
}
public class table3
{
    public decimal col1 { get; set; }
    public decimal col2 { get; set; }
}

然后是另一个包含这些模型列表的模型,如:

public class mymodel
{
    public IEnumerable<table1> table1 { get; set; }
    public IEnumerable<table2> table2 { get; set; }
    public IEnumerable<table3> table3 { get; set; }
}

我想通过以下方式填写mymodel

var model = context.ExecuteStoreQuery<mymodel>("sproc1").FirstOrDefault(); 

mymodel nulltable1table2始终有table3

我怎样才能做到这一点? EF4是否支持它?

1 个答案:

答案 0 :(得分:2)

您无法从EF中的存储过程返回3个结果集。您需要将存储过程拆分为单独的存储过程,每个过程都返回一个结果集(表)。然后你可以得到那些结果

或许您可以更改您的sproc以返回一个结果集,该结果集是3个select语句的UNION,以便可以读取它们 -

select col1, col2 from table1 UNION
select col1, col2 from table2 UNION
select col1, col2 from table3

修改

显然现在支持EF 5 - 请参阅此处获取信息http://msdn.microsoft.com/en-us/data/jj691402