C# 在实体框架中使用存储过程,如何让实体填充其导航属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1035677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:38:56  来源:igfitidea点击:

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

c#.netentity-frameworkentity

提问by user48545

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?

有解决办法吗?

采纳答案by Alex James

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:

并且您有一个获取产品的过程:

i.e.

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:

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

i.e.

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 中的一个功能称为关系修复,一旦第二个实体进入上下文,它就会链接相关实体,将确保在进行两次调用后,产品中的每个产品都将具有非空类别。

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.

另一种方法是使用EFExtensions。编写该文件的人创造了编写一次性加载更多数据的 sproc 的能力。

Hope this helps

希望这可以帮助

Cheers Alex

干杯亚历克斯

回答by Jon P Smith

I found this SO question when researching Stored Procedures (SPs) with EF. I also see people like @KristianNissen and @Todilo have asked if there is an update with EF6.

我在使用 EF 研究存储过程 (SP) 时发现了这个 SO 问题。我还看到像 @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. Nor can you use the .Include() method with SPs as was asked in this SO question.

答案是肯定的,EF 6 已经改变了一些东西,但它没有添加任何东西来帮助在使用 SP 时加载导航属性。您也不能使用 .Include() 方法与 SPs 中所问的这个SO question

The only way is to write your SP to specifically load the navigational properties. However there is now some good Microsoft documentation on using SPs - see Query SPand SP returning multiple result sets.

唯一的方法是编写您的 SP 以专门加载导航属性。但是,现在有一些关于使用 SP 的优秀 Microsoft 文档 - 请参阅查询 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 articleand Entity Framework Tutotial.

为了完整起见,EF 版本 6 带来的更改是允许存储过程 (SP) 处理插入、更新和删除 - 请参阅Microsoft 文章实体框架教程