简体   繁体   English

如何使用Linq查询存储过程对象

[英]How to query a stored procedure object with Linq

Say I got this situation: I have to filter one of my entities with data which I get from an stored procedure: 假设我遇到这种情况:我必须使用从存储过程中获得的数据来过滤我的实体之一:

var results = from c in db.Customer
join p in db.GetSPResults() on c.Id equals p.Id
select c;

on my Context class I got this: 在我的Context类上,我得到了这个:

public ObjectResult<Example> GetSPResults()
{
    return (this as IObjectContextAdapter).ObjectContext.ExecuteFunction<Example>("Proc_Example");
}

So far I run into 2 problems: 到目前为止,我遇到了两个问题:

  1. I get an InvalidOperationException when code strikes the ExcecuteFunction line: 当代码到达ExcecuteFunction行时,我得到一个InvalidOperationException:

    The FunctionImport 'xxx' could not be found in the container 'xxx' . 在容器“ xxx”中找不到FunctionImport“ xxx”

  2. Assuming you guys can help me to solve that problem, would it be possible to query that way? 假设你们可以帮助我解决该问题,是否可以通过这种方式进行查询? Using those stored procedure results like a context entity? 使用这些存储过程的结果就像上下文实体一样? I think EF won't allow that cause it's not an entity, nor a "Constant Value". 我认为EF不允许这样做,因为它既不是实体,也不是“常数值”。

I'm using EF 4.3. 我正在使用EF 4.3。

instead of calling your procedure like that by name as string you can import it in your entity model then you can have a type safe / strongly typed method call directly on your DbContext. 您可以将其导入到实体模型中,而不用像字符串那样按名称来调用过程,然后可以直接在DbContext上进行类型安全/强类型方法调用。

basically you need to execute a function import, see here for an example: Using stored procedures with Entity Framework 基本上,您需要执行函数导入,请参见此处的示例: 在Entity Framework中使用存储过程

Update: for POCO / Code first, see here: EF Code-First - Mapping stored procedures 更新:对于POCO /代码优先,请参见此处: EF代码优先-映射存储过程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM