访问存储过程中获取的相关实体的ObjectContext错误(EF 4)

时间:2012-09-02 21:05:26

标签: stored-procedures entity-framework-4.1 relationship

我有以下商店程序:

ALTER PROCEDURE [dbo].[SP_BuscarBien_servicio]
@campo varchar(50),
@numPags int,
@numRegs int
AS
BEGIN
    SELECT TOP (@numRegs) 
        [Project1].[BS_Id] AS [BS_Id], 
        [Project1].[BS_Descripcion] AS [BS_Descripcion], 
        [Project1].[UNM_Id] AS [UNM_Id], 
        [Project1].[BS_CostoPromedio] AS [BS_CostoPromedio], 
        [Project1].[BSTIP_Id] AS [BSTIP_Id], 
        [Project1].[UNM_Id1] AS [UNM_Id1], 
        [Project1].[UNM_Descripcion] AS [UNM_Descripcion], 
        [Project1].[BSTIP_Id1] AS [BSTIP_Id1], 
        [Project1].[BSTIP_Descripcion] AS [BSTIP_Descripcion]
        FROM ( 
            SELECT [Project1].[BS_Id] AS [BS_Id], 
                   [Project1].[BS_Descripcion] AS [BS_Descripcion], 
                   [Project1].[UNM_Id] AS [UNM_Id], 
                   [Project1].[BS_CostoPromedio] AS [BS_CostoPromedio], 
                   [Project1].[BSTIP_Id] AS [BSTIP_Id], 
                   [Project1].[UNM_Id1] AS [UNM_Id1], 
                   [Project1].[UNM_Descripcion] AS [UNM_Descripcion], 
                   [Project1].[BSTIP_Id1] AS [BSTIP_Id1], 
                   [Project1].[BSTIP_Descripcion] AS [BSTIP_Descripcion], 
                   row_number() OVER (ORDER BY [Project1].[BS_Descripcion] ASC) AS [row_number]
            FROM ( SELECT 
                    [Extent1].[BS_Id] AS [BS_Id], 
                    [Extent1].[BS_Descripcion] AS [BS_Descripcion], 
                    [Extent1].[UNM_Id] AS [UNM_Id], 
                    [Extent1].[BS_CostoPromedio] AS [BS_CostoPromedio], 
                    [Extent1].[BSTIP_Id] AS [BSTIP_Id], 
                    [Extent2].[UNM_Id] AS [UNM_Id1], 
                    [Extent2].[UNM_Descripcion] AS [UNM_Descripcion], 
                    [Extent3].[BSTIP_Id] AS [BSTIP_Id1], 
                    [Extent3].[BSTIP_Descripcion] AS [BSTIP_Descripcion]
                    FROM   [dbo].[Bien_servicio] AS [Extent1]
                    INNER JOIN [dbo].[Unidad_Medida] AS [Extent2] ON [Extent1].[UNM_Id] = [Extent2].[UNM_Id]
                    LEFT OUTER JOIN [dbo].[Bs_tipo] AS [Extent3] ON [Extent1].[BSTIP_Id] = [Extent3].[BSTIP_Id]
                    WHERE [Extent1].[BS_Descripcion] like '%'+ @campo + '%'
                    or [Extent2].[UNM_Descripcion] like '%'+ @campo + '%'
                    or [Extent1].[BS_CostoPromedio] like '%'+ @campo + '%'
                    or [Extent3].[BSTIP_Descripcion] like '%'+ @campo + '%'
                )  AS [Project1]
        )  AS [Project1]
        WHERE [Project1].[row_number] > (@numPags - 1)*@numRegs
        ORDER BY [Project1].[BS_Descripcion] ASC
END

在此功能中导入的内容:

public static List<Bien_servicio> ListarBien_servicio(int numPags, int numRegs, String campo = "")
{
    try
    {
        using (SampleDBEntities db = new SampleDBEntities())
        {
           return db.BuscarBien_servicio(campo, numPags, numRegs).ToList();
        }
    }
    catch (Exception)
    {
        throw;
    }
 }

使用以下关系

Bs_tipo - &lt; Bien_servicio&gt; --- Unidad_Medida

当我尝试获取相关实体(Bien_Servicio.Bs_Tipo)时,我收到错误:“ObjectContext实例已被处理,不能再用于需要连接的操作”

在db层中,相关实体被正确映射,但在winform代码中,我得到了这个异常,就像我丢失了连接一样。

这在存储过程中发生(如果我使用db.Bien_Servicio.Include(“Bs_tipo”)。ToList();我没有问题)。即使我将结果转换为.ToList()。我的错误是什么?

提前致谢。

2 个答案:

答案 0 :(得分:0)

这是一个延迟加载问题。您确实加载了Bien_Servicio列表,但没有加载相关的引用。因此,当您的ListarBien_servicio方法的消费者尝试访问Bien_Servicio.Bs_Tipo时,上下文消失了,请提供您提及的异常。

那么如何加载相关实体?您无法在对存储过程的调用中加载它们,因为存储过程不可组合(这意味着:无法将join转换为sql)。因此,ObjectResult无法支持Include,因为它涉及加入,您也不能在linq中使用join。

您必须在上下文的Bs_Tipo范围内手动Load using个引用。或者您应该让您的存储过程仅返回BS_Id值,并使用Id Select来自db.Bien_Servicio的完整记录(Include)。

答案 1 :(得分:0)

我使用Load解决了这个问题:

var x = (from b in db.BuscarBien_servicio(campo, numPags, numRegs)
        select b).ToList();
foreach (Bien_servicio b in x)
{
     b.Unidad_MedidaReference.Load();
     b.Bs_tipoReference.Load();
}
return x;

而不是

return db.BuscarBien_servicio(campo, numPags, numRegs).ToList();