在实体框架中使用存储过程,如何获取实体以填充其导航属性? [英] Using a stored procedure in entity framework, how do I get the entity to have its navigation properties populated?

查看:82
本文介绍了在实体框架中使用存储过程,如何获取实体以填充其导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实体框架非常慢,所以我尝试使用存储过程,但是遇到了这个问题。

Entity framework is cripplingly slow so I tried using a stored procedure but I ran into this problem.

实体框架允许您定义产生实体的存储过程。但是我的实体具有使用此方法时不会填充的导航属性。

Entity Framework allows you to define a stored procedure that produces an entity. However my entity has 'navigation properties' which are not being populated when using this method.

是否可以解决?

推荐答案

存储过程不可组合。因此,无法使用Include()或其他方法来调用SPROC并让EF自动在同一查询中填充关系。

Well stored procedures are not composable. So there is no way to call your SPROC and have the EF automatically populate relationships in the same query, using Include() or something.

因此,您拥有产品和类别

So say you have products and categories

,您就有了一个程序来获取产品:

and you have a sproc to get Products:

ie

var products = context.GetProducts(someproductfilter);

生成的产品不会加载其类别。

the resulting products won't have their categories loaded.

但是,如果您有第二个存储过程获得了所述产品的类别,则:

However if you have a second stored procedure that gets the Categories for said products:

ie

var categories = context.GetCategoriesForProducts(someproductfilter);

EF中的一个称为关系修正的功能,一旦第二个实体进入上下文,它将链接相关实体确保在两次调用之后,产品中的每个产品都将具有非null的类别。

a feature in EF called relationship fixup, which links related entities once the second entity enters the context, will insure that after both calls are made, each product in products will have a non-null Category.

这不是理想的方法,因为您要执行多个查询,但是

This is not ideal, because you are doing more than one query, but it will work.

另一种方法是使用 EFExtensions 。编写该程序的人创造了编写可以一次加载更多数据的存储过程的能力。

An alternative is to use EFExtensions. The guy who wrote that created the ability to write sprocs that load more data in one go.

希望这会有所帮助

欢呼
亚历克斯

Cheers Alex

这篇关于在实体框架中使用存储过程,如何获取实体以填充其导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆