简体   繁体   English

在实体框架中使用存储过程,如何获取实体以填充其导航属性?

[英]Using a stored procedure in entity framework, how do I get the entity to have its navigation properties populated?

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. 因此,无法使用Include()或其他方法来调用SPROC并让EF在同一查询中自动填充关系。

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);

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. EF中称为关系修正的功能,该功能会在第二个实体进入上下文后链接相关实体,以确保在两次调用之后,产品中的每个产品都将具有非null类别。

This is not ideal, because you are doing more than one query, but it will work. 这是不理想的,因为您要执行多个查询,但是它将起作用。

An alternative is to use EFExtensions . 另一种方法是使用EFExtensions The guy who wrote that created the ability to write sprocs that load more data in one go. 编写该程序的人创造了编写可一次加载更多数据的存储过程的能力。

Hope this helps 希望这可以帮助

Cheers Alex 干杯亚历克斯

I found this SO question when researching Stored Procedures (SPs) with EF. 在使用EF研究存储过程(SP)时,我发现了这个问题。 I also see people like @KristianNissen and @Todilo have asked if there is an update with EF6. 我也看到像@KristianNissen和@Todilo之类的人问过EF6是否有更新。

The answer is yes, EF 6 has changed things, but it did not add anything to help load navigational properties when using SPs. 答案是肯定的,EF 6进行了更改,但没有添加任何内容来帮助使用SP时加载导航属性。 Nor can you use the .Include() method with SPs as was asked in this SO question . 也不能按照此SO问题中的要求将.Include()方法与SP一起使用。

The only way is to write your SP to specifically load the navigational properties. 唯一的方法是编写SP以专门加载导航属性。 However there is now some good Microsoft documentation on using SPs - see Query SP and SP returning multiple result sets . 但是,现在有一些有关使用SP的很好的Microsoft文档-请参阅Query SPSP返回多个结果集

For completeness the change that EF version 6 brought in was to allow Stored Procedures (SPs) to handle insert, update and delete - see Microsoft article and Entity Framework Tutotial . 为了完整起见 ,EF版本6引入的更改是允许存储过程(SP)处理插入,更新和删除-请参阅Microsoft文章Entity Framework Tutotial

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

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