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

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

问题描述

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



实体框架允许你定义一个生成实体的存储过程。然而,我的实体有导航属性,在使用此方法时没有填充。



有没有工作?

解决方案

好的存储过程不可组合。所以没有办法调用您的SPROC,并且使用Include()或其他东西在EF中自动填充相同的查询关系。



所以说你有产品和类别



您有一个sproc可以获得产品:



ie

  var products = context.GetProducts(someproductfilter); 

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



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



ie

  var categories = context.GetCategoriesForProducts(someproductfilter); 

EF中的一个功能称为关系修复,一旦第二个实体进入上下文,它将链接相关实体,将确保在两个电话完成后,产品中的每个产品都将具有非空类别。



这不是理想的,因为您正在执行多个查询,但是它将工作。



另一种方法是使用 EFExtensions 。写的这个人创造了一个写入sprocs的能力,一次加载更多的数据。



希望这有助于



Cheers
Alex


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.

Is there a work around?

解决方案

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:

i.e.

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:

i.e.

var categories = context.GetCategoriesForProducts(someproductfilter);

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.

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

Hope this helps

Cheers Alex

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

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